Developers coming into Kotlin from Scala may sometimes define their function this way:
fun hello() = {
"hello"
}
Calling this function won't print what you expect:
println("Say ${hello()}")
It prints the following:
Say () -> kotlin.String
What we're missing is the second set of parentheses:
println("Say ${hello()()}")
It prints the following:
Say hello
That's because the single-expression definition could be translated into:
fun hello(): () -> String {
return {
"hello"
}
}
It could be further translated into:
fun helloExpandedMore(): () -> String {
return fun(): String {
return "hello"
}
}
Now you can see where that function came from, at least.