Reason: [feature] arg-less switch

Created on 4 Aug 2017  路  8Comments  路  Source: reasonml/reason

switch {
 a > b => ..
 c  => ...
}

which is equivalent to

switch () {
() when a >b => ..
() when c => ..
}
Parser RFC

Most helpful comment

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  => ...

All 8 comments

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 :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gustavopinto picture gustavopinto  路  3Comments

jberdine picture jberdine  路  3Comments

TrakBit picture TrakBit  路  3Comments

modlfo picture modlfo  路  4Comments

chenglou picture chenglou  路  3Comments