February 2018
Intermediate to advanced
350 pages
7h 35m
English
Due to its type inference and expression evaluation, sometimes there are expressions in Kotlin where it is not clear which type is being returned. Most languages resolve this problem by returning the minimum common type between the possible type options. Kotlin takes a different route.
Let's take a look at an example of an ambiguous expression:
fun main(args: Array<String>) { val nullableCupcake: Cupcake? = Cupcake.almond() val length = nullableCupcake?.eat()?.length ?: ""}
What type does length have? Int or String? No, length value's type is Any. Pretty logical. The minimum common type between Int and String is Any. So far, so good. Let's look at the following code now:
val length = nullableCupcake?.eat()?.length ?: ...
Read now
Unlock full access