June 2018
Intermediate to advanced
310 pages
6h 32m
English
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.
Read now
Unlock full access