With sealed classes you can use exhaustive when
expressions and omit the else
clause when the expression returns a result:
sealed c
Our approach avoids to have the function everywhere when auto-completing. With this solution you also have the when return type in compile time so you can continue using functions of the when return type.
Do exhaustive when (sealedClass) {
is SealedClass.First -> doSomething()
is SealedClass.Second -> doSomethingElse()
}
You can define this object like so:
object Do {
inline infix fun exhaustive(any: T?) = any
}