Assume we have the following code:
fun interface A {
fun a()
}
interface B {
fun b(a: A)
}
fun x(callback: A.() -> Unit) = object : B {
override fun b(a: A) {
a.callback()
//callback.invoke(a)
}
}
Why we can use a.callback() instead of callback.invoke(a) What is the meaning of such a syntax? Can't find it in official documentation. And yes, here a.callback() is also equivalent to (a as A).callback()
The type of
callbackis a function type with a receiver. From the documentation:callbackis of typeA.() -> Unit, so it can be called on an instance ofA(i.e. the parametera).This is similar to how you normally call extension functions.
callbackhere acts like an extension function onA(because its type isA.() -> Unit), so you can call it like an extension function insidex.