MR3Y
03/10/2024, 12:08 PMwhen
in the following code snippet smart enough to infer it is already exhaustive? my searching skills are failing me here.
fun main() {
printFoo(Foo.C)
}
fun printFoo(parameter: Foo) {
if (parameter is Foo.C || parameter is Foo.Bar) {
// Compiler error: 'when' expression must be exhaustive, add necessary 'A', 'B' branches or 'else' branch instead
val message = when(parameter) {
is Foo.C -> "C"
is Foo.Bar.D -> "D"
is Foo.Bar.E, is Foo.Bar.F -> ""
}
println(message)
}
}
sealed interface Foo {
data object A : Foo
data object B : Foo
data object C : Foo
sealed interface Bar : Foo {
data object D : Bar
data object E : Bar
data object F : Bar
}
}
ephemient
03/10/2024, 12:12 PM