sealed interface Foo
sealed interface Bar
sealed interface Thing {
object ONE : Thing, Foo
object TWO : Thing, Bar
}
fun main(){
val thing: Thing = Thing.ONE
if(thing is Foo && thing is Bar){
// This can´t happen
}
}
c
Chris Lee
10/25/2022, 4:26 PM
Kotlin when statements are exhaustive over sealed types; if statements are not.
Chris Lee
10/25/2022, 4:30 PM
why do you believe that can’t happen? an object can implement multiple interfaces; there’s nothing preventing implementation of Foo and Bar.
Chris Lee
10/25/2022, 4:31 PM
Copy code
sealed interface Foo
sealed interface Bar
sealed interface Thing {
object ONE : Thing, Foo
object TWO : Thing, Bar
object THREE : Thing, Foo, Bar
}
fun main(){
val thing: Thing = Thing.ONE
if(thing is Foo && thing is Bar){
}
}
j
Joffrey
10/25/2022, 4:41 PM
@Chris Lee these interfaces are sealed, so adding
object THREE
could make the code path viable, it still doesn't mean that the compiler cannot detect dead code without it (in theory)
m
Michael de Kaste
10/26/2022, 7:18 AM
@Joffrey Yeah, I feel like, since
when
knows what a foo is in its statement and knows how to fill in the rest, I feel like an if on on these two disjoint sets would be an "always false" statement