-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Description
The "switch" statement is a staple of many programming languages. It facilitates a more concise and readable syntax for conditional behaviour than the "if" statement.
In Radiance conditional syntax is an expression, allowing the body to emit a value. As such "switch" should also be an expression that can return a value. It is important that this behaviour be optional, and should carry no additional cost for those who do not wish to use it. To emit a value the switch expression must be exhaustive, either by having a case for every possible variant or a fallback case if a variant is not matched. A possible further addition could be to return an "Optional" value from the expression if it is not exhaustive.
The traditional syntax definitely has room for improvement, both Rust and Swift are good examples of a modern syntax for switch/match:
- No support for fall through behaviour by default.
- More concise definition of cases that match multiple variants.
- No need for explicit "break" statements.
- Reassignment or destructuring of the input parameter on a case by case basis.
In addition to these syntax features the compiler should provide exhaustiveness checks.
Enums partner very well with switch expressions in general, as they are easy to check for exhaustiveness and variants often require different behaviour. In fact the combination of an enum and switch expression make an excellent foundation for a FSM.