Skip to content

Latest commit

 

History

History
42 lines (29 loc) · 1.02 KB

language_switch.mdx

File metadata and controls

42 lines (29 loc) · 1.02 KB
idkeywordsnamesummarycategory
switch
switch
pattern
match
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.

Example

<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();

References

close