An alternative to the switch statement

If I understand you correctly, you meant that if a switch doesn't
have a default, it signals to us that it takes care of all possible
cases, at least when we have an enum. Admittedly, my proposed
solution doesn't do that.

It was maybe a bit cryptic when I said in a previous message
that I don't consider the "default: break" to be exhaustive. What
I meant was:

It's as if someone would ask me what I will do tomorrow, and my
answer would be: I will eat lunch at noon – and as for the rest of
the day – I'm not going to say anything about that.

That doesn't feel exhaustive. But, this is more about semantics
than anything else.

Here's another example of the should/be/else, with a different
formatting style:

should someInt be 1 { function1()
} be 2 { function2()
} be 3 { function3()
}

One possible solution for the "signal problem":

If the switch object is an enum with a limited number
of possible values, and not all cases are covered, then
an else block must be included. The else block could
also be left empty:

should someEnum be .a { function1()
} be .b { function2()
} be .c { function3()
} else {
}
1 Like