Skip to content

Latest commit

 

History

History
46 lines (32 loc) · 944 Bytes

language_switch.mdx

File metadata and controls

46 lines (32 loc) · 944 Bytes
id keywords name summary category
switch
switch
pattern
match
switch
This is the `switch` pattern matching keyword.
languageconstructs

A switch expression allows you to execute some code based on the shape of the data.

Example

<CodeTab labels={["ReScript", "JS Output"]}>

type shape = Circle(float) | Square(float)

let shape = Square(3.0)

let message = switch shape {
| Circle(radius) => "Circle with radius " ++ Js.Float.toString(radius)
| Square(length) => "Square with sides of length " ++ Js.Float.toString(length)
}
var shape = {
  TAG: /* Square */ 1,
  _0: 3.0,
};

var message;

message =
  shape.TAG === /* Circle */ 0
    ? "Circle with radius " + (3.0).toString()
    : "Square with sides of length " + (3.0).toString();

References