I have a question about "exhaustive when expression": if I have an enum value which is guaranteed not to have a specific value, the compiler still insists on a branch for that value. Is there a way to avoid that? Concrete simplified example (which does not compile unless I uncomment the one commented out line):
Copy code
import kotlin.random.Random
enum class Priority { LOW, MEDIUM, HIGH }
fun main() {
val priority = Priority.values()[Random.nextInt(Priority.values().size)]
val s1 = when (priority) {
Priority.LOW -> 1
Priority.MEDIUM -> 2
Priority.HIGH -> 3
}
val s2: Int
if (priority == Priority.LOW) {
s2 = 1
} else {
s2 = when(priority) {
// Priority.LOW -> throw IllegalStateException()
Priority.MEDIUM -> 2
Priority.HIGH -> 3
}
}
println("s1=$s1 s2=$s2")
}
nkiesel
02/10/2021, 9:04 PM
I tried that also with 1.4.30 and the new backend but I get the same result