I have a class defined like this:
sealed class Animal {
class Cat(val isFluffy: Boolean): Animal()
class Dog: Animal()
}
And a test function like this, which doesn’t currently compile:
fun isFluffyCat(someAnimal: Animal): Boolean {
when (someAnimal) {
is Animal.Dog -> return false
}
return someAnimal.isFluffy
}
However, if I change it to this, it compiles:
fun isFluffyCat(someAnimal: Animal): Boolean {
when (someAnimal) {
is Animal.Dog -> return false
is Animal.Cat -> {} // no-op, just here to satisfy the compiler
}
return someAnimal.isFluffy
}
It seems like it should be possible for the compiler to infer type on a nearly-exhaustive when
clause if all other types trigger a return or throw an exception. Is this a change planned for a future release?