Quick Answer
Concise syntax for functional parameters
Understanding the Issue
Kotlin lambdas provide a concise way to pass function literals. They can be written outside parentheses when last parameter, and support implicit "it" parameter.
The Problem
This code demonstrates the issue:
Kotlin
Error
// Problem: Verbose anonymous class
view.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View) {
// ...
}
})
The Solution
Here's the corrected code:
Kotlin
Fixed
// Solution: Lambda expression
view.setOnClickListener { view ->
// ...
}
// With implicit 'it' parameter:
view.setOnClickListener {
it.isVisible = false
}
// Storing lambdas:
val square: (Int) -> Int = { x -> x * x }
Key Takeaways
Use lambdas for concise functional programming patterns.