In IntelliJ IDEA, the following code produces the compile error Error: ‘when’ expression must be exhaustive, add necessary ‘else’ branch:
sealed class Foo (x: Int) {}
class Bar (x: Int): Foo(x) {}
class Baz (x: Int): Foo(x) {}
fun <T: Foo> quux(x: T): String {
return when (x) {
is Foo -> "Foo"
is Bar -> "Bar"
is Baz -> "Baz"
}
}
But since Foo is a sealed class, then T must be a Foo, Bar or Baz, so the when-expression is exhaustive. Is this a compiler bug?