DRAFTThis page is not published. Only visible in development mode.

Enums

An enum is a named, closed set of variants declared as schema. Columns typed by an enum can only hold one of its variants - the database enforces what would otherwise be a stringly-typed convention in application code.

Declaring and using an enum

Declare the variants once, then use the enum as a column type. Values are written as variant paths - dm_enum::status::Active - so a typo is a schema error, not silent bad data. Under the hood each variant is stored as a compact tag (its index), which is what a plain read shows. Run the snippets on this page in order:

$ A Plain Enum as a Column Type
$ ctrl+enter to run

Variants with payloads

Variants can carry typed fields, making an enum a full sum type. A column holding Circle { radius } or Rectangle { width, height } stores exactly one variant's payload; reads flatten the variant tag and every possible field, with none for fields of the variants not present:

$ Enum Variants Can Carry Payloads
$ ctrl+enter to run

Enum, dictionary, or plain string?

  • --Fixed set known at design time, enforced by the schema: enum.
  • --Open set discovered at runtime, stored compactly: a dictionary.
  • --Free-form text: a plain utf8 column.
Related type declarations
The same variant syntax powers two sibling primitives: tags classify series entries, and events dispatch to handlers.