Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Enums

⚠️ NOTE: the current syntax to define enums is still type X { .. }. This will be changed soon.

You can also create your own types, specifying its constructors:

enum MyType {
  A,
  B(Int, Int),
}

In addition to creating a type called MyType, we can also use its constructors:

// inferred as `MyType`
let x = A

// inferred as `MyType`
let y = B(0, 1)

Note: unlike languages like Ocaml, constructors are first class values. For example, in this case, B is a function of type (Int, Int) -> MyType

Custom types can have generic args:

enum Box<a> {
  MakeBox(a),
}

Notable types

There are a few types that are implemented as enums in the standard library instead of being built-in: