How would the new "when with guards" ensure exhaustiveness.
d
Davio
06/11/2024, 12:57 PM
a branch with a guard is ignored for exhaustiveness checks so you'd need to always include an else or don't use guards, but use nested conditions instead
thank you color 1
a
Alejandro Serrano.Mena
06/17/2024, 3:54 PM
not exactly, it may be the case that your when is exhaustive even removing the exhaustiveness. For example, you can do
Copy code
when (val n = ...) {
null -> ...
is Int if n > 0 -> ...
is Int
}
and this would cover every case for
n: Int?
d
Davio
06/18/2024, 7:43 AM
well in that case there are 2 cases without guards so they could be taken into account for exhaustiveness checks, so maybe in this case the compiler is smart enough to figure that out, but if that's true, than the last case could be replaced by
else
and it would be the same thing
p
Pablichjenkov
06/18/2024, 12:45 PM
IMHO not a great addition, the fact that messes up with exhaustiveness, rather confusing