January 2018
Intermediate to advanced
434 pages
14h 1m
English
Lambda functions are functions that are not declared but passed as an expression. In Kotlin, if a function receives an interface, we can replace it with a lambda. For example, the setOnClickListener function receives View.OnClickListener, so we can use a lambda:
fun setOnClickListener(listener: (View) -> Unit)someView.setOnClickListener({ view -> doSomething() })
Also, if there are no parameters to be passed to the lambda function, we can omit the arrow, and if the last parameter being passed is a function, we can move it outside the parentheses:
someView.setOnClickListener() { doSomething() }
Then, if the lambda function being passed is actually the only parameter, you can omit the parentheses completely:
button.setOnClickListener ...
Read now
Unlock full access