Run
Not related to threads in any way, run()
is much like let()
, but it sets the context to this
instead of using it
:
val justAString = "string" val n = justAString.run { this.length }
Usually, this
could be omitted:
val n = justAString.run { length }
It is mostly useful when you plan to call a number of methods on the same object, much like apply()
.
The return result, unlike apply()
, may be of a totally different type, though:
val year = JamesBond().run { name = "ROGER MOORE" movie = "THE MAN WITH THE GOLDEN GUN" 1974 // <= Not JamesBond type }