Module lege.enum

Allows the creation of enumerations.

Lege enums are immutable, meaning trying to add or change enum values after their creation is an error, and detect accesses to undefined enum values.

Values can have any underlying type, but their keys must be strings.

Usage:

    local enum = require "lege.enum"
    
    -- Define an enum
    local Color = enum 'Color' {
    -- Strings with numeric keys define enum values
    "red",
    "green",
    "blue",
    -- String keys allow you to define a different value
    dark_orange = "red",
    -- Values don't have to be strings, but keys do
    black = 0,
    }
    
    print(Color) --> enum Color: 0xdeadbeaf
    
    assert(Color.red == "red")
    assert(Color.green == "green")
    assert(Color.blue == "blue")
    assert(Color.dark_orange == "red")
    assert(Color.black == 0)
    
    -- Trying to access an undefined enum value is an error
    -- print(Color.white) --> Error: no value 'white' on enum 'Color'
    -- Likewise trying to set a value is an error
    -- Color.White = 255 --> Error: attempt to set value 'White' on enum 'Color'
    

Meta Methods

__newindex Raises an error caused by trying to modify an enum value.
__tostring Convert an enum type to a human-readable string.

Functions

returns... ()
_enum_err_nf (self, key) Raises an error caused by access to an undefined enum value.
enum_body (values) Define an enum's values.
enum (name) Create an enum type.


Meta Methods

__newindex
Raises an error caused by trying to modify an enum value.

param:

  • self enum The enum instance
  • key string The key that was undefined
  • value The value that you tried to set
__tostring
Convert an enum type to a human-readable string. Currently we merely output the enum's name and address.

param:

  • self enum The enum to convert to a string

Functions

returns... ()

Returns:

    A function enum for creating new enumeration types.
_enum_err_nf (self, key)
Raises an error caused by access to an undefined enum value.

Parameters:

  • self enum The enum instance
  • key string The key that was undefined

Raises:

An error indicating which value was undefined, and on which enum type
enum_body (values)
Define an enum's values. A closure over an enum's name returned from enum which creates a new metatable for the enum type, and allows you to define what values the struct has.

Parameters:

  • values table Values for the new struct type

Returns:

    enum The new enum type
enum (name)
Create an enum type.

Parameters:

  • name string The enum's type name (used when converting to string)

Returns:

    A closure over name enum_body to allow you to specify the enum's values
generated by LDoc 1.5.0 Last updated 2024-03-04 22:59:08