You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced/language-extensions.md
+32-6Lines changed: 32 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -274,7 +274,7 @@ You can also map functions to table accessors (`__index` and `__newindex`). See
274
274
275
275
## Lua Table Types
276
276
277
-
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 lua method tables are translated to simple lua:
277
+
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 Lua method tables are translated to simple Lua:
278
278
279
279
-`table.get(key)` Get a value by key -> `table[key]`
280
280
-`table.set(key, value)` Set a value for key -> `table[key] = value`
@@ -328,7 +328,7 @@ print(#tbl)
328
328
329
329
</SideBySide>
330
330
331
-
### Iterating
331
+
### Iterating over `LuaTable`
332
332
333
333
To iterate over a `LuaTable`, use `for...of`. This will generate a `for...in` statement using `pairs()`.
334
334
@@ -366,11 +366,37 @@ end
366
366
367
367
### `LuaMap` and `LuaSet`
368
368
369
-
Similar to `LuaTable`, the `LuaMap` and `LuaSet` types are provided to represent Lua tables used as a map or set. These are much more performant than the `Set`/`Map` classes, but do not come with all the same features (such as guaranteed insertion order).
369
+
Similar to `LuaTable`, the `LuaMap` and `LuaSet` types are provided to represent Lua tables that are used as a map or a set. These are more performant than the `Set`/`Map` classes, but do not come with all of the same features (such as guaranteed insertion order).
370
370
371
-
-`LuaMap` has the same methods as `LuaTable`, except that `table.get(key)` may also return `undefined`.
372
-
-`LuaSet`, instead of `table.get/table.set`, has `table.add(value)`, which translates to `table[value] = true`.
373
-
- There are also the readonly variants `ReadonlyLuaMap` and `ReadonlyLuaSet`.
371
+
-`LuaMap` has the same methods as `LuaTable`. The exception is that the type of the return value for the `get` method is `V | undefined` instead of `V`, which makes it similar to how a `Map` works. (For this reason, we recommend using `LuaMap` over `LuaTable`, as it provides compiler-safety guarantees whenever you access a value.)
372
+
-`LuaSet` does not have `get` and `set` methods. Instead, it has the `add` method, which transpiles to `table[value] = true`.
373
+
- TSTL also provides the read-only variants of `ReadonlyLuaMap` and `ReadonlyLuaSet`, if needed.
374
+
375
+
#### Iterating over `LuaMap` & `LuaSet`
376
+
377
+
If you need to iterate over a `LuaMap` or a `LuaSet`, you do so in roughly the same way that you would for `LuaTable`:
378
+
379
+
```ts
380
+
const luaMap =newLuaMap();
381
+
for (const [key, value] ofluaMap) {
382
+
}
383
+
384
+
const luaSet =newLuaSet();
385
+
for (const value ofluaSet) {
386
+
}
387
+
```
388
+
389
+
(Under the hood, both of these for loops would transpile to using `pairs` to iterate over the table.)
390
+
391
+
#### Should I use `Map`/`Set` or `LuaMap`/`LuaSet`?
392
+
393
+
`Map`/`Set` support more features than `LuaMap`/`LuaSet`. Namely, they have the `keys`, `entries`, `values`, and `clear` methods, a `size` attribute, and consistent iteration order (in the order of insertion). On the other hand, `LuaMap` and `LuaSet` transpile directly to a Lua table, so they are more lightweight.
394
+
395
+
Thus, you might want to use `LuaMap`/`LuaSet`:
396
+
397
+
- when the table needs to be serialized
398
+
- when you need to interact with other Lua libraries
399
+
- when you are in a critical path where the marginal improved performance would really matter (measure first!)
0 commit comments