February 2018
Intermediate to advanced
350 pages
7h 35m
English
Inline lambda functions have an important restriction—they can't be manipulated in any way (stored, copied, and others).
The UserService stores a list of listeners (User) -> Unit:
data class User(val name: String)class UserService { val listeners = mutableListOf<(User) -> Unit>() val users = mutableListOf<User>() fun addListener(listener: (User) -> Unit) { listeners += listener }}
Changing addListener into an inline function will produce a compilation error:
inline fun addListener(listener: (User) -> Unit) { listeners += listener //compilation error: Illegal use of inline-parameter listener}
If you think about it, it makes sense. When we inline a lambda, we're replacing it for its body, and that isn't something that ...
Read now
Unlock full access