Kotlin will do exhaustive checks in a `when` block when it is an expression. You can add a `.let { }` at the end of the when statement if you want to enforce this and not really return anything.
sealed class Communication
data class Email(val emailAddress: String) : Communication()
data class Shouting(val preferredName: String) : Communication()
object HiveMind : Communication()
fun exhaustiveWhen(comm: Communication) {
// not exhaustively checked (will compile)
when (comm) {
is Email -> println("sending email to ${comm.emailAddress}")
}
// *is* exhaustively checked (error at compile time)
// must add HiveMind or 'else' branch
val result = when (comm) {
is Email -> println("sending email to ${comm.emailAddress}")
is Shouting -> println("HELLO, ${comm.preferredName}!")
}
// *is* exhaustively checked
when (comm) {
is Email -> println("sending email to ${comm.emailAddress}")
is Shouting -> println("HELLO, ${comm.preferredName}!")
}.let { }
}