https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
n

natpryce

12/27/2016, 10:49 PM
Hi. With the following sealed class hierarchy, the function
foo
doesn’t compile in any version of Kotlin (I’ve tried up to 1.1-M4). The compiler complains that the when expression is not exhaustive, although it actually is.
Copy code
sealed class Base {
	sealed class Mid : Base() {
        class A: Mid()
        class B: Mid()
    }
    class C: Base()
}


fun foo(b: Base): String = 
   when (b) {
       is Base.Mid.A -> "A"
       is Base.Mid.B -> "B"
       is Base.C -> "C"
    }
I think that the exhaustiveness check for sealed classes should take into account whether intermediate subclasses are also sealed.