July 2018
Intermediate to advanced
400 pages
12h 14m
English
Next up on our tour of the standard functions is run. run is similar to apply in that it provides the same relative scoping behavior. However, unlike apply, run does not return the receiver.
Say you wanted to check whether a file contains a particular string:
val menuFile = File("menu-file.txt")
val servesDragonsBreath = menuFile.run {
readText().contains("Dragon's Breath")
}
The readText function is implicitly performed on the receiver – the File instance. This is just like the setReadable, setWriteable, and setExecutable functions you saw with apply. However, unlike apply, run returns the lambda result – here, a true or false value.
run can also be used to execute a function reference on a receiver. You used function ...
Read now
Unlock full access