Java programmers will be used to "casting": converting a reference to some
generic type (e.g., Animal
) to a more specific type (e.g., Axolotl
).
The Kotlin compiler frequently can automatically cast objects, through
"smart casts".
In this snippet, the compiler knows that critter
is a Frog
in the
is Frog
branch of the when
. So, it automatically casts critter
to be a Frog
, so we can safely call hop()
on it. The same holds
true for the smart cast of critter
to Axolotl
in the is Axolotl
branch.
You can learn more about this in:
Tags: