I’ve found a cunning trick to get the Kotlin compi...
# intellij
n
I’ve found a cunning trick to get the Kotlin compiler to check when statements for exhaustiveness when the branches return Unit. E.g. if I have a sealed Fruit class with sub-classes Apple, Banana and Clementine, this will not be checked for exhaustiveness:
Copy code
when (fruit) {
   is Apple -> printApple(fruit)
   is Banana -> printBanana(fruit)
}
But if you add a non-null assertion at the end, it will be treated as an expression, and then the compiler will check for exhaustiveness, so the code below will not compile, as one would wish.
Copy code
when (fruit) {
   is Apple -> printApple(fruit)
   is Banana -> printBanana(fruit)
}!!
But IntelliJ marks the !! with an unused code warning.