Quick Answer

Anonymous functions as first-class citizens

Understanding the Issue

Kotlin lambdas are concise anonymous functions that can be passed as arguments or stored in variables. They support implicit parameter (it) and trailing lambda syntax.

The Problem

This code demonstrates the issue:

Kotlin Error
// Problem: Anonymous object
view.setOnClickListener(object : View.OnClickListener {
    override fun onClick(v: View) {
        doSomething(v)
    }
})

The Solution

Here's the corrected code:

Kotlin Fixed
// Solution: Lambda expression
view.setOnClickListener { view -> doSomething(view) }

// With implicit parameter:
view.setOnClickListener { doSomething(it) }

// Storing lambdas:
val square: (Int) -> Int = { x -> x * x }

Key Takeaways

Use lambdas for concise event handlers and functional operations.