switch {
a > b => ..
c => ...
}
which is equivalent to
switch () {
() when a >b => ..
() when c => ..
}
what would be the reason to use this over if/else?
That's kinda neat. Pushing people toward switch is nice. Though it seems to trigger Warning 25: bad style, all clauses in this pattern-matching are guarded which we don't suppress
You can do a syntax check that argless switch requires a default branch
In the erlang ecosystem this construct is known as cond, if you want a different name for it (although switch seems fine for it too if no ambiguities pop up).
Could the syntax be something different to help avoid visual ambiguity and to emphasize that a catch-all case is not something to encourage in normal switch expr { | case => ... }uses?
Maybe:
switch if {
| a > b => ...
| c < d => ...
| else => ... /* "else" is a keyword so maybe safe to use here? */
}
It could desugar to if/elses or switch with a catch-all case replacing the else case.
Is if/else not good enough?
switch {
a > b => ..
c => ...
}
vs
if (a > b) {
...
} else if (c) {
...
}
?
switch { and switch () { won't be possible in master since it conflicts, I think. But this might work:
if {
| a > b => ..
| c => ...
}
Or drop the brackets:
if
| a > b => ..
| c => ...
I actually would much prefer if this were in if rather than switch, so personally I think it's a good thing that it conflicts :)
Most helpful comment
switch {andswitch () {won't be possible in master since it conflicts, I think. But this might work:Or drop the brackets: