Skip to content

Commit e8fae59

Browse files
authored
more luamap documentation (#115)
* more luamap documentation * update * Update language-extensions.md * Update language-extensions.md * prettier * more prettier * Update language-extensions.md * Update language-extensions.md
1 parent aa8c2b0 commit e8fae59

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

docs/advanced/language-extensions.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ You can also map functions to table accessors (`__index` and `__newindex`). See
274274

275275
## Lua Table Types
276276

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:
278278

279279
- `table.get(key)` Get a value by key -> `table[key]`
280280
- `table.set(key, value)` Set a value for key -> `table[key] = value`
@@ -328,7 +328,7 @@ print(#tbl)
328328

329329
</SideBySide>
330330

331-
### Iterating
331+
### Iterating over `LuaTable`
332332

333333
To iterate over a `LuaTable`, use `for...of`. This will generate a `for...in` statement using `pairs()`.
334334

@@ -366,11 +366,37 @@ end
366366

367367
### `LuaMap` and `LuaSet`
368368

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).
370370

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 = new LuaMap();
381+
for (const [key, value] of luaMap) {
382+
}
383+
384+
const luaSet = new LuaSet();
385+
for (const value of luaSet) {
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!)
374400

375401
### Custom Getters and Setters
376402

0 commit comments

Comments
 (0)