Reputation: 13
In Kotlin, what does this syntax mean ?
class HomeActivity : AppCompatActivity() {
val examplesAdapter = HomeOptionsAdapter {
val fragment = it.createView()
...
}
I just thought Anonymous function or Lambdas ...
Upvotes: 1
Views: 442
Reputation: 29260
I you check the HomeOptionsAdapter
you'll see in the constructor this
class HomeOptionsAdapter(val onClick: (ExampleItem) -> Unit)
so it takes an onClick listener, a function that takes a ExampleItem
as input and returns Unit
. This is a Kotlin language feature, you can place a lambda outside the ()
if it's the last parameter. These are equivalent:
val examplesAdapter = HomeOptionsAdapter({
val fragment = it.createView()
...
})
val examplesAdapter = HomeOptionsAdapter {
val fragment = it.createView()
...
}
Upvotes: 3