https://kotlinlang.org logo
#announcements
Title
# announcements
t

thana

06/03/2019, 10:12 AM
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

ribesg

06/03/2019, 10:14 AM
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

hho

06/03/2019, 10:15 AM
I use the
.exhaustive()
trick explained here: https://proandroiddev.com/til-when-is-when-exhaustive-31d69f630a8b
👍 2
t

thana

06/03/2019, 10:19 AM
wow this trick's great. thank you!
r

ribesg

06/03/2019, 10:22 AM
Yeah really nice trick
g

gildor

06/03/2019, 10:34 AM
Unfortunately it pollutes code-completion
I prefer use `.hashCode()`/`toString()` for the same 😬
t

thana

06/03/2019, 10:36 AM
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

gildor

06/03/2019, 10:37 AM
I agree %)
h

hho

06/03/2019, 10:41 AM
maybe something like
.also { /* check exhaustiveness */ }
… and have that as a live template …
👍 3
g

gildor

06/03/2019, 10:42 AM
good idea actually
j

Jonathan Mew

06/03/2019, 11:56 AM
what is a 'live template'?
really like this idea in principle 🙂
3 Views