July 2018
Intermediate to advanced
400 pages
12h 14m
English
First on our tour of the standard functions is apply. apply can be thought of as a configuration function: It allows you to call a series of functions on a receiver to configure it for use. After the lambda provided to apply executes, apply returns the configured receiver.
apply can be used to reduce the amount of repetition when configuring an object for use. Here is an example of configuring a file instance without apply:
val menuFile = File("menu-file.txt")
menuFile.setReadable(true)
menuFile.setWritable(true)
menuFile.setExecutable(false)
Using apply, the same configuration can be achieved with less repetition:
val menuFile = File("menu-file.txt").apply { setReadable(true) setWritable(true) setExecutable(false) } ...Read now
Unlock full access