Is there a "clean" way to enforce an exhaustivenes...
# announcements
t
Is there a "clean" way to enforce an exhaustiveness check for
when(someSealedClass)
? I just came across a piece of code where it was very natural to use then
when
without any assignment, hence the compiler would never complain when a branch was missing 🤔
i mean, we can rewrite the code so we assign a the outcome of
when
to a variable, but this way the code actually gets more complicated
r
There’s a warning in the IDE for that, you can configure it to be error level, but I don’t think it’ll prevent compilation with Gradle. I don’t know if another tool can do that
h
I use the
.exhaustive()
trick explained here: https://proandroiddev.com/til-when-is-when-exhaustive-31d69f630a8b
👍 2
t
wow this trick's great. thank you!
r
Yeah really nice trick
g
Unfortunately it pollutes code-completion
I prefer use `.hashCode()`/`toString()` for the same 😬
t
yeah that's true. OTOH
checkForExhaustiveness
at least tells what it is good for. i think
hashcode
is so undescriptive that one day some developer could think about what that is good for and simply remove it, because it doesnt have any obvious use
g
I agree %)
h
maybe something like
.also { /* check exhaustiveness */ }
… and have that as a live template …
👍 3
g
good idea actually
j
what is a 'live template'?
really like this idea in principle 🙂