Passing lambdas as the only or last argument

When a lambda is passed to a function as the only or last argument, enclosing it in parentheses is optional. Let's go back to our click listener example. We could have also written it like this:

button.setOnClickListener { v -> println("Clicked on: $v") }

This feature enhances readability and is particularly useful if you are creating a DSL (Domain-Specific Language) with Kotlin.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.108.22