July 2018
Intermediate to advanced
400 pages
12h 14m
English
Kotlin’s type inference rules behave exactly the same with function types as they do with the types you met earlier in this book: If a variable is given an anonymous function as its value when it is declared, no explicit type definition is needed.
This means that the anonymous function you wrote earlier that accepted no arguments:
val greetingFunction: () -> String = {
val currentYear = 2018
"Welcome to SimVillage, Mayor! (copyright $currentYear)"
}
Could also have been written with no specified type, like this:
val greetingFunction = {
val currentYear = 2018
"Welcome to SimVillage, Mayor! (copyright $currentYear)"
}
Type inference is also an option when the anonymous function accepts one or more ...
Read now
Unlock full access