rrva
06/05/2020, 11:48 AMwhen
over a type system, like
when {
foo is A ->
foo is B ->
}
why can the compiler not figure out compile-time if the when is exhaustive without an else branch?diesieben07
06/05/2020, 11:49 AMMilan Hruban
06/05/2020, 11:51 AMsindrenm
06/05/2020, 12:24 PMwhen (foo)
instead:
sealed class Foo
object A : Foo()
object B : Foo()
fun main() {
val foo: Foo = A
// This won't work, as there are other
// potential cases that doesn't involve
// `foo`.
// val bar = when {
// foo is A -> TODO()
// foo is B -> TODO()
// }
val bar = when (foo) {
is A -> TODO()
is B -> TODO()
}
}
when
, even if foo
is an instance of a sealed class that only has an A
and B
subclass, isn't exhaustive,when
is covered. Another potential case of your when
could be this, for instance:
when {
foo is A -> TODO()
foo is B -> TODO()
0 == 0 -> TODO() // <--
}