dephinera
03/01/2021, 9:21 AMfun foo(block: String.(String) -> Unit) {
}
fun foo(block: () -> Unit) {
}
fun main() {
foo { it: String ->
// calls foo(block: String.(String) -> Unit)
}
foo { // ERROR
// how to call foo(block: () -> Unit)
}
}
Basically is there a way to specify in the second case that I want to call foo(block: () -> Unit)
. When I specified the type of the lambda argument, it seemed that the compiler was able to figure it out, however it: Unit
doesn't work for the second case.Shawn
03/01/2021, 10:30 AMfoo { ->
// do stuff here
}
dephinera
03/01/2021, 10:31 AMYoussef Shoaib [MOD]
03/01/2021, 10:45 AMlambdaLiteral (used by annotatedLambda, functionLiteral)
: '{' statements '}'
| '{' lambdaParameters? '->' statements '}'
;that question mark means 0 or 1 of the item, and lambdaParameters defines the whole list of parameters, and so according to the grammar you can have no parameters but still use the
->
before your statements. So I guess it is documented and expected behaviour, it's just that no one actually mentions it anywhere outside of the grammar!dephinera
03/01/2021, 11:28 AMAdam Powell
03/01/2021, 3:14 PM