so i’m creating a sealed interface but for some re...
# random
m
so i’m creating a sealed interface but for some reason the exhaustive when clause is kicking in for abstract implementations:
Copy code
sealed interface A {
    abstract class AbstractA: A
    class B: AbstractA()
    class C: AbstractA()
}

fun x(a: A) {
    when (a) {
        is A.B -> { }
        is A.C -> { }
    }
}
Has anyone else noticed this before? I get a compile error on the when saying i need to add an implementation for AbstractA, even though it can never be anything but B or C since those are the only concrete implementations.
e
AbstractA
is not sealed, so anybody could add a
class D : A.AbstractA()
outside of your sealed interface, thus you do need to check for it
m
I tried making it private and that doesn’t work either. Honest, i just wanted to have a base class because these two implementations happen to share a lot of data fields in common, with a slight variation of one or two fields for each type.
a
In other words, change your code into
Copy code
sealed interface A {
    sealed class AbstractA: A
    class B: AbstractA()
    class C: AbstractA()
}

fun x(a: A) {
    when (a) {
        is A.B -> { }
        is A.C -> { }
    }
}
m
ok. that makes more sense.
thanks @andylamax