← Back to index

ReScript Polymorphic Variants

Sun Dec 13 2020

ReScript version: bs-platform@8.4.2

Polymorphic variants are distinguished from ordinary variants by the leading hash.

Unlike ordinary variants, polymorphic variants can be used without an explicit type declaration.

Example:

let status = #yes

let statusString = {
  switch status {
  | #yes => "Yes"
  | #no => "No"
  }
}

However they may be assigned to a type:

type color = [#red | #green | #blue]

let colorToString = (color: color): string => {
  switch color {
  | #red => "Red"
  | #green => "Green"
  | #blue => "Blue"
  }
}