They have only syntactic difference, run
is an extension function while with
is not. Here are the definitions (in kotlin-sdlib:1.0.3
):
public inline fun <T, R> T.run(block: T.() -> R): R = block() // equivalent to this.block()
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
Since run
is an extension function, it has one more implicit argument of type T
, so the argument types are the same. The bodies of the functions are also effectively the same.
Their performance should also be equivalent since both are inline
functions: the resulting bytecode should only contain the inlined block
body.
The differences in the functions usage are all caused by the fact that run
is an extension.
First, run
is suitable for calls chaining:
foo.run { bar }.run { baz }
Second, and more important, if a declared variable type has run
function with the same signature, it will be called instead of the extension. And run
can be shadowed by another extension. This is how extensions are resolved. Example:
class MyClass {
fun <R> run(blockIgnored: MyClass.() -> R): Nothing = throw RuntimeException()
}
"abcdefg".run { println("x") } // prints "x"
MyClass().run { println("x") } // throws RuntimeException
(MyClass() as Any).run { println("x") } // prints "x"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…