2 ms·
Kotlin's smart cast is nice in this context. One example is with nullable types. if you have val foo: Whatever? // might be null and Whatever and Whatever? ar
by jillesvangurp 7y ago
Kotlin's smart cast is nice in this context.
One example is with nullable types.
if you have
val foo: Whatever? // might be null and Whatever and Whatever? are two separate types.
foo.someMethod()// compilation error because foo is not of type Whatever
if(foo != null) {
foo.someMethod() // works because foo was smart cast to Whatever after the null check
}
In the same way doing a switch on a type smart casts the variable to that type. Smart casting of nullable types also gets rid of a lot of clumsy things like Optional, Maybe, etc. you'd need in other languages where you'd have to call a method to extract the value, assign it to a new variable, etc.