Skip to content

Commit 203c0e9

Browse files
authored
iterating over LuaTable (#43)
* iterating over LuaTable * fixing linter
1 parent 4d56f67 commit 203c0e9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/advanced/language-extensions.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ You can also map functions to table accessors (`__index` and `__newindex`). See
230230

231231
## Lua Table Types
232232

233+
### Getting and Setting
234+
233235
The `LuaTable` type is provided to allow direct creation and manipulation of Lua tables. This is useful if you want to use a table that uses types other than string for its keys, as that is not supported by Typescript. Calls to `get` and `set` on the table will transpile directly to `value = table[key]` and `table[key] = value`.
234236

235237
Example:
@@ -266,12 +268,35 @@ print(#tbl)
266268

267269
</SideBySide>
268270

271+
### Iterating
272+
273+
To iterate over a `LuaTable`, use `pairs()`. (This requires the `lua-types` library to be installed.)
274+
275+
```ts
276+
const tbl = new LuaTable();
277+
278+
tbl.set(3, "bar");
279+
tbl.set(4, "bar");
280+
tbl.set(5, "bar");
281+
282+
for (const [key, value] of pairs(tbl)) {
283+
print(key);
284+
print(value);
285+
}
286+
```
287+
288+
(Remember that in Lua, `pairs()` returns the keys in a random order.)
289+
290+
### Restricting the Types
291+
269292
`LuaTable` can also be restricted to use only certain types as keys and values:
270293

271294
```ts
272295
const tbl = new LuaTable<KeyType, ValueType>();
273296
```
274297

298+
### Custom Getters and Setters
299+
275300
If you have a type that uses non-string keys, you can use `LuaTableGet` and `LuaTableSet` function types to declare your own getters & setters, similar to [Operator Map Types](#operator-map-types).
276301

277302
Example:

0 commit comments

Comments
 (0)