Skip to main content

Enumerated Types

When modeling state machines it can be useful to describe variants of a single type that can contain different values. This concept is simple enough to describe optional types or complex enough to model multi-state systems.

Definition

Use the enum keyword to define enumerated types with one or more variants.

knot
enum CardSuit =
| Spade
| Diamond
| Club
| Heart;
knot
enum CardSuit =
| Spade
| Diamond
| Club
| Heart;

Constructor Arguments

Each enum variant can optionally specify some data that it must take as arguments during construction, and can be extracted when destructuring.

knot
enum Primitive =
| Nil
| Boolean(boolean)
| Integer(integer)
| Float(float)
| String(string);
knot
enum Primitive =
| Nil
| Boolean(boolean)
| Integer(integer)
| Float(float)
| String(string);