Main navigation

Learning Kotlin: Smart Casts

This is the 9th post in a multipart series.
If you want to read more, see our series index

The goal of this Koan is to show how smart the Kotlin compiler is; in that when you use something like the is keyword to handle type checking the compiler will then know the type later on and be able to use it intelligently.

So if we want to check types in Java we would use something like this where we would use instanceof to check the type and then cast it to the right type.

In Kotlin we, by checking the type the compiler handles the casting for us, but before we get to that we also got to learn about the when which is the Kotlin form of the Switch keyword in C# or Java and it offers similar functionality, as shown in this example:

when(x){

1-> print("x == 1")

2-> print("x == 2")

else->{// Note the block

print("x is neither 1 nor 2")

}

}

and it supports multiple values on the same branch

when(x){

0, 1-> print("x == 0 or x == 1")

else-> print("otherwise")

}

Where it gets awesome, is the extra actions it supports; for example when values can be functions, not just constants:

when(x){

parseInt(s)-> print("s encodes x")

else-> print("s does not encode x")

}

You can also use in or !in to check values in a range/collection:

when(x){

in1..10-> print("x is in the range")

in validNumbers -> print("x is valid")

!in10..20-> print("x is outside the range")

else-> print("none of the above")

}

It really is very cool, so let us see how we use Smart Casts and when together and how it compares with the Java code above: