Strange lambda function that works

Hi guys can you explain me how this works :

fun showSnack(parent : View, message : String, listener : View.OnClickListener){
Snackbar.make(parent, message, Snackbar.LENGTH_LONG)
.setAction(“Confirm”, listener)
.show()
}

and when i call this with

showSnack(parentCat, “my string”) { frag?.launchDeleteBasket() }

So i am developping in Android Studio with kotlin 1.4.0.
it works fine, but my third argument was an OnCLickListener, and why it put the third argument outside. Normally my third argument should be a

() → Unit

. But if I do so it will not work because setAction() require View.OnClickListener.

Thanx

It’s a combination of 2 well known language features
trailing lambdas:
https://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter

and SAM conversion:
https://kotlinlang.org/docs/reference/fun-interfaces.html#sam-conversions

1 Like

Thank you