This is similar to the previous one, but it checks directly for the type:
if (nullableCupcake is Cupcake) {
nullableCupcake.eat()
}
It also works with when:
when (nullableCupcake) {
is Cupcake -> nullableCupcake.eat()
}
Both options, checking for null and non-null types, are a little bit verbose. Let's check other options.