I can write a lambda expression outside of parenthesis, but I cannot put it there by name. I have tried many ways:
val plus3: (Int,Int,Int)->Int = {a,b,c->a+b+c}
println(apply3(1,2,3){a,b,c->a+b+c}) // OK
println(apply3(1,2,3){plus3}) // Type mismatch. Required: Int, Found: (Int,Int,Int)->Int
println(apply3(1,2,3){(plus3)}) // Type mismatch. Required: Int, Found: (Int,Int,Int)->Int
println(apply3(1,2,3)plus3) // unresolved reference
println(apply3(1,2,3){plus3()}) // value captured in a closure
println(apply3(1,2,3){(plus3)()}) // value captured in a closure
What is the syntax to put a name there (outside of parenthesis)?
I don't know why, but in the documentation there is not a word on the theme. It says we could put lambda there, but not a word about a variable or constant that denotes that lambda.
Yes, there is:
plus3
is an identifier and not a lambda expression, so you can't specify it outside of parentheses.You mean the error messages when you pass
{ plus3 }
? By Kotlin rules{ plus3 }
is a lambda which ignores its argument (if any) and returnsplus3
. So the rule applies, andapply3(1,2,3){plus3}
means the same asapply3(1,2,3,{plus3})
.Exactly the opposite: it expects to see an
Int
as the return value of the lambda and seesplus3
which is(Int,Int,Int) -> Int
.That was exactly my point: the rule is purely syntactic, it's applied before the compiler knows anything about type or value of
plus3
, and so it doesn't know or care whether this value happens to be a lambda.The rule could instead say
in which case
apply3(1,2,3) plus3
would work. But it doesn't.Placing a lambda expression outside of a function call's parentheses is the same as placing it inside the parentheses like this:
From here, we can simply assign the lambda to a
val
(as you have done) which results in: