Skip to content

Table API

Kevin Tyrrell edited this page Oct 27, 2024 · 3 revisions

Table class

Read-only utility class for modifying or creating tables

read_only(private, meta_methods)

Constructs a read-only view into a table

Read-only tables cannot be modified. Mutating the resulting table will result in Error.UNSUPPORTED_OPERATION. getmetatable yields a function which returns the length of the underlying table (equivalent to #private. Calling # on the read-only table is unsupported due to __len meta method not being present in Lua 5.1.

Parameter Type Description Requirement
private table Internal fields of the table Required
meta_methods table Meta methods to be included into the resulting table Optional
  • returns [table] Read-only wrapper of specified table

put_default(tbl, key, default_value)

Fetches the key’s value, or sets and returns a default value if absent

If default value is dynamic, use put_compute(tbl, key, computer).

Parameter Type Description Requirement
tbl table Table to query Required
key Any Key of the pairing Required
default_value Paired value if the key is absent Required
  • returns [Any] Resulting value of the key-value pairing

put_compute(tbl, key, computer)

Fetches the key’s value, or sets and returns a computed value if absent

If the computed value is a constant, use put_default(tbl, key, default_value).

Parameter Type Description Requirement
tbl table Table to query Required
key Any Key of the pairing Required
computer function [Any] callback([Any]) Required
  • returns [Any] Resulting value of the key-value pairing

set(...)

Constructs a set of specified values

Each key of the set is associated with true. nil elements are ignored from the set.

Parameter Type Description Requirement
... Any Elements of the resulting set Required
  • returns [table] Set of specified values

sort(tbl, comparator)

Sorts a table using the specified comparator

Implementation uses two-part recursive Quicksort. This sort is not stable. Runtime should be similar to Lua's table.sort.

Parameter Type Description Requirement
tbl table Table to be sorted Required
comparator function [Number] callback([Any], [Any]) Required
  • returns nil