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
ephemient
10/24/2022, 9:39 PM
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
mattinger
10/24/2022, 9:49 PM
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
andylamax
10/24/2022, 9:49 PM
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 -> { }
}
}