October 2025
Intermediate to advanced
100 pages
2h 27m
English
The filterIsInstance function fails to remove the non-matching menu items, and both lines of output display all the unfiltered data. Enjoy drinking your pizza!
The angle-bracketed parameter that distinguishes a Set<Food> from a Set<Drink> is obvious to us in the code, but it turns out not to be included in the compiled program. At runtime, all three Set objects have the same type.
Kotlin’s compiler normally prohibits impossible type checks like this, but the filterIsInstance function adds enough indirection to mask the problem.
Like the array we used in Puzzle 8, What’s in the Box?, Set<Food> is a generic type. It’s made up of a base type, Set, and a type parameter, Food.
When you compile your program, the type ...
Read now
Unlock full access