With
Unlike the other four scoping functions, with()
is not an extension function.
This means you cannot do the following:
"scope".with { ... }
Instead, with()
receives the object you want to scope as an argument:
with("scope") { println(this.length) // "this" set to the argument of with() }
And as usual, we can omit this
:
with("scope") { length }
Just like run()
and let()
, you can return any result from with()
.