table
Methods on every table.
Call these with .$
A table's slots and its methods share one namespace, and a slot wins. A table with a key called len, rawget or keys hides the method of that name, and the ordinary call then fails with attempt to call 'string' or whatever the slot happens to hold. Nothing warns, because a table with a key called len is legal.
Writing the call as t.$len() reaches the built-in type method directly and never looks at the slots, so it cannot be broken by the data:
let squad = { len = "a field, not a method" }
squad.len() // throws: attempt to call 'string'
squad.$len() // 1
Plain t.len() is fine for a table whose keys you wrote yourself. For anything parsed from a file, received over a network, or handed in by a caller, prefer .$. The same applies to class and instance members, which share their namespace the same way. See the .$ operator.
Functions
| __merge | pure (table|class).__merge(other: table|instance|class, ...): any | Returns a new table built from this table and every argument, later arguments overriding earlier ones. |
| __update | (table|class).__update(other: table|instance|class, ...): any | Copies every slot from each argument into the table, in place, later arguments overriding earlier ones. |
| clear | clear(): any | Removes every key/value pair from the table, in place. |
| clone | nodiscard clone(): any | Returns a shallow copy of the table. |
| constructor | constructor(): any | Returns a new, empty table. |
| each | table.each(callback: function): any | Calls callback once for every slot in the table. |
| filter | nodiscard table.filter(callback: function): any | Returns a new table holding only the slots for which callback returns a truthy result. |
| findindex | pure table.findindex(callback: function): any | Returns the key of the first value for which callback returns a truthy result. |
| findvalue | pure table.findvalue(callback: function, [default], ...): any | Returns the first value for which callback returns a truthy result. |
| hasindex | pure table.hasindex(key): any | Reports whether key is a slot in the table. |
| hasvalue | pure table.hasvalue(value): any | Reports whether value is stored under any key in the table. |
| is_frozen | nodiscard is_frozen(): any | Reports whether this reference to the table was frozen with freeze(). |
| keys | pure table.keys(): any | Returns an array of every key in the table. |
| len | pure table.len(): any | Returns the number of key/value pairs in the table. |
| map | nodiscard table.map(callback: function): any | Returns a new table with the same keys, each mapped through callback. |
| rawdelete | table.rawdelete(key): any | Removes the slot at key. |
| rawget | pure table.rawget(key): any | Returns the value stored under key. |
| rawin | pure table.rawin(key): any | Reports whether key is a slot in the table. |
| rawset | table.rawset(key, value): any | Sets the slot at key to value, creating the slot if it does not exist yet. |
| reduce | nodiscard table.reduce(callback: function, ...): any | Combines every value in the table into one result by calling callback repeatedly. |
| replace_with | table.replace_with(other: table): any | Replaces the table's entire contents with a copy of other's slots, in place. |
| swap | table.swap(key1, key2): any | Exchanges the values stored at key1 and key2. |
| topairs | nodiscard table.topairs(): any | Returns an array of [key, value] pairs, one per slot in the table. |
| tostring | nodiscard tostring(): any | Returns a string that names the type and identity of the table. |
| values | pure table.values(): any | Returns an array of every value in the table. |
| weakref | nodiscard weakref(): any | Returns a weak reference to the table. |