February 2020
Intermediate to advanced
412 pages
9h 36m
English
The apply() function is similar to let(), but, 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. This is helpful in declaring an item as T but doing tangential operations on it without breaking the declaration/assignment flow.
Here is a non-reactive 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 (the ch12_19.kt example):
fun main(args: Array<String>) { val myItem = MyItem().apply { startProcess() } } class MyItem { fun startProcess() = println("Starting ...Read now
Unlock full access