Named Tuple List
It’s often desirable to be able to store lists of associated values without needing to strongly
define each inner object and encapsulate them within individual tables. The NamedTupleList
class provides an easy-to-use mechanism for this.
API
Below is an example which demonstrates how to use a NamedTupleList.
local MyModule = select(2, ...).MyModule
local NamedTupleList = MyModule:From("LibTSMUtil"):IncludeClassType("NamedTupleList")
local players = NamedTupleList.New("name", "level", "className")
players:InsertRow("Bob", 10, "Warrior")
players:InsertRow("Jeff", 12, "Mage")
players:InsertRow("Tim", 7, "Mage")
print(players:GetNumRows()) -- 3
player:SetRowField(2, "level", row:GetRowField(2, "level") + 1)
print(players:GetRow(2)) -- Jeff 13 Mage
Memory Management
The NamedTupleList objects are intended to never be GC’d and have a static lifecycle (i.e. one
that’s equal to the lifecycle of the application), but there is nothing preventing them from being
GC’d.
-
class NamedTupleList:
Class -
staticmethod New(...:
string):NamedTupleList Creates a new named tuple list.
- Parameters:
... (
string) – The tuple field names, in order
-
InsertRow(self:
NamedTupleList, ...:any):number Insert a row into the list and returns its index.
- Parameters:
... (
any) – The row values
-
GetNumRows(self:
NamedTupleList):number Gets the number of rows in the list.
-
GetRow(self:
NamedTupleList, rowIndex:number): (...:unknown) Gets the data for a given row.
- Parameters:
rowIndex (
number) – The row index
-
GetRowField(
self:NamedTupleList,
rowIndex:number,
fieldName:string
):any Gets a single field for a given row.
- Parameters:
rowIndex (
number) – The row indexfieldName (
string) – The name of the field
-
SetRowField(
self:NamedTupleList,
rowIndex:number,
fieldName:string,
value:any
) Sets a single field for a given row.
- Parameters:
rowIndex (
number) – The row indexfieldName (
string) – The name of the fieldvalue (
any) – The field value
-
RemoveRow(self:
NamedTupleList, rowIndex:number) Removes a row at a given index.
NOTE: This will invalidate other row indexes.
- Parameters:
rowIndex (
number) – The row index
-
Iterator(self:
NamedTupleList): fun():number, ...unknown,table,number Iterates over each row in the list.
- Returns:
_1 (fun():
number, ...unknown) – Iterator with fields: rowIndex, …
-
Wipe(self:
NamedTupleList) Wipes the list.
-
staticmethod New(...: