The only way to make when exhaustive (for sealed classes) is to use it as an expression. The problem is, a lot of times there is no result (or in other words, result is Unit).
So I end up writing:
val dummy = when (x) {
is A -> doA()
is B -> doB()
is C -> doC()
}
(the other option is to refactor as a separate function, which is inconvenient in many cases)
Unfortunately, this bothers the compiler because dummy is unused.
My suggestion is to allow special variable _ in exhaustive when
I know why you want this. Using when as an expression is the only way (right now) to ensure that the compiler checks that it is exhaustive.
That being said I don’t like your suggestion to handle it as it feels like a hack and not like a feature. Instead I would take a look at this topic. There is a discussion about the same problem. I like @fvasco’s suggestion