With sealed classes you can use exhaustive when expressions and omit the else clause when the expression returns a result:
sealed class SealedClass {
class First : SealedClass()
class Second : SealedClass()
}
fun test(sealedClass: SealedClass) : String =
when (sealedClass) {
is SealedClass.First -> "First"
is SealedClass.Second -> "Second"
}
Now if I were to add a Third to SealedClass, the compiler will complain that the when expression in test() is not exhaustive, and I need to add a clause for Third or else.
I am wondering however if this check can also be enforced when test() does not return anything:
fun test(sealedClass: SealedClass) {
when (sealedClass) {
is SealedClass.First -> doSomething()
is SealedClass.Second -> doSomethingElse()
}
}
This snippet does not break if Third is added.
I can add a return statement before when, but this could easily be forgotten and may break if the return type of one of the clauses is not Unit.
How can I make sure I don't forget to add a branch to my when clauses?
Any?, and force a return value, but it still feels a bit hacky. - nhaarmanrun. I have expanded the question a little with an example. - nhaarman