This case is not considered exhaustive and results...
# language-evolution
k
This case is not considered exhaustive and results in compilation error:
Copy code
when (Pair(bool1, bool2) {
    Pair(false, false) -> {...}
    Pair(false, true) -> {...}
    Pair(true, false) -> {...}
    Pair(true, true) -> {...}
}
Is it just because it's too difficult for the compiler to figure out that it is in fact exhaustive?
d
Yes, it's too difficult For compiler
Pair
is just a regular class, like anyone else
👍 2
c
What you’re thinking of is Pattern Matching, which is not supported yet, though there’s been discussions on it for a while (see https://youtrack.jetbrains.com/issue/KT-186). It’s a very complicated language feature, and the Kotlin team has never (to my knowledge) committed to actually implementing it, though they haven’t said “no” to it either. Kind of in a limbo where we may get it eventually, but don’t hold your breath waiting for it. The thing I typically do in this kind of case is just throw an
else -> error("will never happen")
line in the when, knowing that the equality checks will be exhaustive even if the compiler cannot figure it out
m
There was a nice article sometime ago that was diving into that and how difficult it was. Can't find it anymore but maybe someone reading this will?