Exhaustive cases
A switch expression can be used to return a value or just execute a set of statements, like the traditional switch statement.
When you are using a switch expression to return a value that is used to assign a value to a variable, its cases must be exhaustive. This essentially means that, whatever value you pass to the switch argument, it must be able to find an appropriate branch to execute. A switch expression can accept arguments of the byte, short, int, Byte, Short, Integer, or String types or enums. Of these, only an enum has exhaustive values.
In the following example, a switch expression is being used to assign a value to the damage variable. Since there is no matching branch to execute for the PLATE value, this code ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access