Is there annotation (1.2.x) to force compiler to r...
# announcements
a
Is there annotation (1.2.x) to force compiler to rise error on not-exhaustive
when
?
n
isn’t that the default behavior? but the compiler needs to be able to infer it’s not exhaustive (e.g. enum)
k
That's only the behavior for expression `when`s, not statements. And I agree that is something that's missing.
For now you can do something like
inline fun Any?.exhaustive() = Unit
, use like this:
Copy code
when (x) { ... }.exhaustive()
It forces the
when
to be an expression.
Problem is that that pollutes the global namespace.
a
I have found even more handy workaround:
Copy code
val <T> T.exhaustive: T
    get() = this
here: https://proandroiddev.com/til-when-is-when-exhaustive-31d69f630a8b But I have hoped something official is possible (that is to treat a warning as compile time error).
k
Meh, why would you want that? Either you use it as an expression and then you don't need any tricks, and otherwise you don't need the
.exhaustive()
to return anything.
a
For readability (that is to express intention explicitly) at all cases. But you are right, of course, it isn't obligatory. Still not sure which way to use. I guess, it is matter of taste 🙂
k
No definitely don't do anything funky when you actually want the expression, just use the language feature then!
o
I also found it useful for side effect
when
that I still want to handle all cases
k
Okay, but then just do something like
Copy code
val Any?.exhaustive
    get() = Unit
no need to make it more confusing.