1

From what I've learned, it seems like extension function T.run and with have kind of the same purpose of creating the possibility to group multiple calls on the same object, returning lambda's last object as its result.

T.run() has the advantage of applying check for nullability before using it. (as this article points it out)

What are the advantages of using with? or better said: What stops me from always using T.run() instead? Thanks

DoruAdryan
  • 1,314
  • 3
  • 20
  • 35

3 Answers3

1

As you've said, they function identically, other than the ability of using run with a safe call, and using it in chained expressions, for example:

foo.bar().run { qwert() }

The real difference is in the syntax - use whichever provides better readability in your code for you.

zsmb13
  • 85,752
  • 11
  • 221
  • 226
1

This is the case for many scope functions, you can't always tell which one is "correct" to be used, it's often the developer's choice actually. As for with and run, the only difference is how the receiver of the scope function comes into play:

On the one hand, the receiver is passed as an argument to with:

val s: String = with(StringBuilder("init")) {
    append("some").append("thing")
    println("current value: $this")
    toString()
}

On the other hand, run directly gets called on the receiver (extension function):

val s: String = StringBuilder("init").run {
    append("some").append("thing")
    println("current value: $this")
    toString()
} 

run has the advantage of nullability-handling since the safe operator can be applied:

val s: String = nullableSB?.run {
   //...
} ?: "handle null case"

I haven’t seen many usages of run whereas with is more commonly used I think.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
0

You can put with onto next line, which can be more readable if you have long initializer:

val obj = VeryLongInitializer().DoSomething().AnotherThing()
with (obj) {
    // Do stuff
}

vs

val obj = VeryLongInitializer().DoSomething().AnotherThing().run {
    // Do stuff
}
Matej Drobnič
  • 981
  • 10
  • 18