June 2017
Intermediate to advanced
400 pages
8h 44m
English
A tool similar to let() is apply(). Instead of turning a T item into an R item,which let() does, apply() executes a series of actions against the T item instead, before returning the same T item itself. This is helpful in declaring an item T but doing tangential operations on it without breaking the declaration/assignment flow.
Here is a nonreactive example. We have a simple class, MyItem, which has a startProcess() function. We can instantiate MyItem but use apply() to call this startProcess() method before assigning MyItem to a variable, as shown in the following code:
fun main(args: Array<String>) { val myItem = MyItem().apply { startProcess() } } class MyItem { fun startProcess() = println("Starting Process!") }
The output ...
Read now
Unlock full access