Discussion
Our mysteryFootprints variable has a type of Yeti? and a value of null, both of which are possible branches in our when block. But instead of selecting one of those two branches, our program takes the first branch. That’s because null is a valid value for every nullable type and satisfies all four branches of the when block. When more than one branch is matched, Kotlin picks the first matching branch and ignores the rest.
Nullable Type Checks
As you saw in Puzzle 1, Something from Nothing?, null represents a missing value—an empty slot where data would normally be stored.
If you’ve programmed in Java, you might be used to treating null as an oddball outlier in your type declarations and runtime type checks. Java’s instanceof keyword—which ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access