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

enum

import enum

This module provides Zuri’s implementation of enumerations as described in the language documentation: a set of unique values bound to symbolic names via an alias.

Creating an enumeration

Initialize from a list of symbolic names; each is automatically assigned an ordinal value starting from zero:

import enum

var Gender = enum(['Male', 'Female'])
echo Gender.Male     # 0
echo Gender.Female   # 1

Initialize from a dictionary to bind specific values (numbers or strings) instead of automatic ordinals:

var Color = enum({
  Red: 'r',
  Green: 'g',
  Blue: 'b',
})
echo enum.to_string(Color)   # <Enum Red=r Green=g Blue=b>

By default, duplicate values in a dictionary-initialized enumeration raise an error. Pass true as the second argument to allow them:

var Speed = enum({
  Slow: 1,
  Sluggish: 1,
  Fast: 2,
}, true)

The enum API

Every public name in enum, wherever it is declared. Each links to the page that documents it.

NameKindSummary
enum.ensurefunctionReturns value unchanged if it is a valid value for the enumeration, or raises an Error if it is not.
enum.enumfunctionCreates a new enumeration from either a list of symbolic names or a dictionary of symbolic-name to value…
enum.hasfunctionReturns true if the enumeration contains the given symbolic key, or false otherwise.
enum.keysfunctionReturns the symbolic keys of an enumeration, in the order they were declared.
enum.to_dictfunctionReturns the enumeration as a key/value dictionary.
enum.to_stringfunctionReturns a string representation of the enumeration.
enum.to_value_dictfunctionReturns the enumeration as a value/key dictionary.
enum.valuesfunctionReturns the possible values of an enumeration, in the order their corresponding keys were declared.

Functions

enum()

enum.enum(source, allow_duplicates) -> dict

Creates a new enumeration from either a list of symbolic names or a dictionary of symbolic-name to value pairs.

When source is a list, every item must be a string; each is bound, in order, to an automatically assigned ordinal value starting at zero.

When source is a dictionary, its keys must be strings and its values must be numbers or strings. Duplicate values are rejected unless allow_duplicates is true.

Enumeration keys are always unique; since the source is itself a dictionary or list, a duplicated symbolic name is never possible to express in the first place.

Parameters

  • source (list[string]|dict)
  • allow_duplicates (?bool) — Default value is false.

Returns dict

Raises TypeError: If source is not a list or dictionary, or its entries are of the wrong type.

Raises Error: If a duplicate value is found and allow_duplicates is not true.

keys()

enum.keys(e) -> list[string]

Returns the symbolic keys of an enumeration, in the order they were declared.

Parameters

  • e (dict)

Returns list[string]

Note: Equivalent to calling .keys() directly on the enum object, since an enumeration is itself a dictionary.

values()

enum.values(e) -> list[number|string]

Returns the possible values of an enumeration, in the order their corresponding keys were declared.

Parameters

  • e (dict)

Returns list[number|string]

Note: Equivalent to calling .values() directly on the enum object.

to_dict()

enum.to_dict(e) -> dict

Returns the enumeration as a key/value dictionary.

Parameters

  • e (dict)

Returns dict

to_value_dict()

enum.to_value_dict(e) -> dict

Returns the enumeration as a value/key dictionary.

Since dictionaries cannot contain duplicate keys, if multiple enumeration keys share the same value, only the LAST declared key for that value is represented in the result; matching the documented behavior.

Parameters

  • e (dict)

Returns dict

has()

enum.has(e, key) -> bool

Returns true if the enumeration contains the given symbolic key, or false otherwise.

Parameters

  • e (dict)
  • key (string)

Returns bool

Note: Equivalent to calling .contains(key) directly on the enum object.

ensure()

enum.ensure(e, value) -> any

Returns value unchanged if it is a valid value for the enumeration, or raises an Error if it is not.

Parameters

  • e (dict)
  • value (any)

Returns any

Raises Error: If value is not one of the enumeration’s values.

to_string()

enum.to_string(e) -> string

Returns a string representation of the enumeration.

Parameters

  • e (dict)

Returns string


2026, Richard Ore and The Zuri Contributors