Docs / Language Manual / Extensible Variant
Edit

Extensible Variant Types

Variants can be made extensible by defining a type using .. and adding new variant constructors using +=.

ReScriptJS Output
 
type t = ..

type t += Other

type t +=
  | Point(float, float)
  | Line(float, float, float, float)

Pattern-matching is possible the same way as with normal variants but there is one caveat: With extensible variants, the possibility to check for exhaustiveness vanishes, you always need a default case _ here.

ReScriptJS Output
 
let print = v =>
  switch v {
  | Point(x, y) => Js.log2("Point", (x, y))
  | Line(ax, ay, bx, by) => Js.log2("Line", (ax, ay, bx, by))
  | Other
  | _ => Js.log("Other")
  }