I have a sealed class hierarchy like so:
sealed class Base
class A : Base()
abstract class B : Base()
class C : Base()
class D : Base()
Then I want to have an exhaustive when
:
fun main() {
val c: Base = C()
when (c) {
is A -> println("A")
is C -> println("C")
is D -> println("D")
}.let {}
}
But this is not allowed because
'when' expression must be exhaustive, add necessary 'is B' branch or 'else' branch instead
But that's not true! I've covered all possibilities. There is no value that isn't matched by my when.
Furthermore, if I do add a is C
clause:
when (c) {
is A -> println("A")
is B -> println("B")
is C -> println("C")
is D -> println("D")
}.let {}
It isn't actually called. C
is printed.
This is actually a problem in the other direction, too. Say I want the same logic to handle all subclasses of B
: C
and D
:
when (c) {
is A -> println("A")
is B -> println("some subclass of B")
}.let {}
That's also not allowed, even though it is exhaustive. What's going on here? Is this a bug in Kotlin? Am I missing something?