What is the name of the syntactical feature where ...
# compiler
m
What is the name of the syntactical feature where you can scope a
Unit
to be a sort of anonymous extension function on a given class? I noticed it in the Compose code (I think) but I haven't been able to find it in the docs. Example:
Copy code
fun render(node: Node, action: Node.() -> Unit) {
    node.action()
}
e
in the example,
render
is a higher-order function taking a function type (or lambda) with receiver. https://kotlinlang.org/docs/lambdas.html
m
Thanks for the link! It looks like "function literal with receiver" is the specific terminology I'm looking for here. Thanks! https://kotlinlang.org/docs/lambdas.html#function-literals-with-receiver
e
a function literal starts with
fun
, which is not the only way to get a function type - a lambda expression
{ ... }
also does
the example there is just to show that you can explicitly write the receiver type of a function literal, while with a lambda expression it can only be inferred
any function can have a receiver or not
m
Ah ok.
Copy code
Lambda expressions can be used as function literals with receiver when the receiver type can be inferred from the context.
So if I'm understanding the terminology, in this case the lambda is not strictly speaking a "function literal" (since it is not defined as
fun
) but is simply being used in the same way?
e
yes
👍🏻 1
"lambda with receiver" is probably the most common description for what you're asking for, but I don't think the docs mention it - that phrasing is a bit inaccurate
👍🏻 1
m
That makes sense. Thanks for explaining!