52

Learning Kotlin: Smart Casts

 5 years ago
source link: https://www.tuicool.com/articles/hit/Z3yaAja
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The goal of this Koan is to show how smart the Kotlin compiler is. When you use something like the is keyword to handle type checking, the compiler will 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.

public class JavaCode8 extends JavaCode {
public int eval(Expr expr) {
if (expr instanceof Num) {
return ((Num) expr).getValue();
}
if (expr instanceof Sum) {
Sum sum = (Sum) expr;
return eval(sum.getLeft()) + eval(sum.getRight());
}
throw new IllegalArgumentException("Unknown expression");
}
}

In Kotlin, we check the type of compiler handles being cast for us, but, before we get to that, we also need to learn about the when   Kotlin form of the Switch keyword in C# or Java. 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")
}
}

It also 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) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..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:

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK