Learning Kotlin: Smart Casts

Submitted by Robert MacLean on Tue, 06/26/2018 - 09:00

More Information

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.

  1. public class JavaCode8 extends JavaCode {
  2.     public int eval(Expr expr) {
  3.         if (expr instanceof Num) {
  4.             return ((Num) expr).getValue();
  5.         }
  6.         if (expr instanceof Sum) {
  7.             Sum sum = (Sum) expr;
  8.             return eval(sum.getLeft()) + eval(sum.getRight());
  9.         }
  10.         throw new IllegalArgumentException("Unknown expression");
  11.     }
  12. }

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:

  1. when (x) {
  2.     1 -> print("x == 1")
  3.     2 -> print("x == 2")
  4.     else -> { // Note the block
  5.         print("x is neither 1 nor 2")
  6.     }
  7. }

and it supports multiple values on the same branch

  1. when (x) {
  2.     0, 1 -> print("x == 0 or x == 1")
  3.     else -> print("otherwise")
  4. }

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

  1. when (x) {
  2.     parseInt(s) -> print("s encodes x")
  3.     else -> print("s does not encode x")
  4. }

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

  1. when (x) {
  2.     in 1..10 -> print("x is in the range")
  3.     in validNumbers -> print("x is valid")
  4.     !in 10..20 -> print("x is outside the range")
  5.     else -> print("none of the above")
  6. }

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:

  1. fun eval(e: Expr): Int =
  2.     when (e) {
  3.         is Num -> e.value
  4.         is Sum -> eval(e.left) + eval(e.right)
  5.    }

Really nice and, I think, more readable than the Java code.