https://kotlinlang.org logo
Title
d

david-wg2

09/11/2018, 8:52 AM
private fun whenTest(a: Boolean, b: Boolean) = when {
    a && b -> ""
    !a && b -> ""
    a && !b -> ""
    !a && !b -> ""
}
i'm being told this when expression isn't exhaustive, and that it needs an
else
is this a bug?
e

edwardwongtl

09/11/2018, 8:59 AM
Maybe the
or
cases?
d

david-wg2

09/11/2018, 9:02 AM
this is an exhaustive truth table, no matter what other ways of checking the variables you introduce, it will be covered by one of these checks
p

Pavlo Liapota

09/11/2018, 9:06 AM
just use
else ->
instead of
!a && !b ->
🙂
d

david-wg2

09/11/2018, 9:07 AM
i know i can do that, but the compiler is lying
e

edwardwongtl

09/11/2018, 9:10 AM
Ah I think I know what’s going on. It asks for a
else
because there is no bound for
when
, sth like
when (someEnum) { ... }
. So the condition branches can be any statement that results in a
Boolean
. Even though you’re branches covers all possibilities for
a
and
b
, there is still infinite possibilities out there that does not involve `a`/`b`.
d

david-wg2

09/11/2018, 9:12 AM
fair point
e

edwardwongtl

09/11/2018, 9:13 AM
Technically it is not a bug, but maybe some room for improvement.
1
d

david-wg2

09/11/2018, 10:57 AM
i created an issue for it (a feature request), but it turns out it's already registered as a bug: https://youtrack.jetbrains.net/issue/KT-20943