Hi team! I understand what extension functions are...
# announcements
j
Hi team! I understand what extension functions are, such as adding a newFunction
Copy code
fun Bar.newFunction() {}
What exactly does this do?
Copy code
fun Bar.() {}
m
That code is not valid 🤔
j
It is though! We've had it in production... for a few months and the person that wrote it is no longer at the company haha
m
image.png
j
Screen Shot 2021-03-18 at 08.23.19.png
Screen Shot 2021-03-18 at 08.23.35.png
c
thats the return parameter of the method
m
As an expression, okay. It means to create/return a function (closure/lambda) that expects an
Application
instance to be passed as receiver (
this
).
c
Copy code
val x: Bar.() -> Unit = {}
thats valid
j
ah, so it's a javascript bind
Thanks!
m
Copy code
val foo1 = fun Bar.() {}
fun Bar.foo2() {}
Is roughly the same. Just that the former is an extension lambda and the second a real extension function.
j
Gotcha. Thanks Marc!
m
Similar to JS, yes. Just in JS you cannot specify the type of
this
.
Bar
in this case.
z
i think the kotlin docs distinguish between “anonymous functions” (e.g.
fun () {}
) and “lambdas” which don’t use the
fun
keyword, but yea they’re effectively the same thing