id | keywords | name | summary | category | |||
---|---|---|---|---|---|---|---|
switch |
| switch | This is the `switch` expression keyword. | languageconstructs |
A switch
expression allows you to match / destructure almost any kind of value (array, list, records, strings, numbers, variants, etc). Each branch (pattern) of a switch
expression must return a value of the same type.
<CodeTab labels={["ReScript", "JS Output"]}>
typeshape=Circle(float) | Square(float) letshape=Square(3.0) letmessage=switchshape { | Circle(radius) =>"Circle with radius "++Float.toString(radius) | Square(length) =>"Square with sides of length "++Float.toString(length) }
varshape={TAG: "Square",_0: 3.0};varmessage;message=shape.TAG==="Circle" ? "Circle with radius "+(3.0).toString() : "Square with sides of length "+(3.0).toString();