Skip to content

Commit f4965b2

Browse files
committed
Add docs for LuaMap and LuaSet
1 parent 0605921 commit f4965b2

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

docs/advanced/language-extensions.md

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ Example:
7070
<SideBySide>
7171

7272
<!-- prettier-ignore -->
73+
7374
```ts
74-
for (const i of $range(1, 5)) {}
75-
for (const i of $range(1, 10, 2)) {}
76-
for (const i of $range(5, 1, -1)) {}
75+
for (const i of $range(1, 5)) {
76+
}
77+
for (const i of $range(1, 10, 2)) {
78+
}
79+
for (const i of $range(5, 1, -1)) {
80+
}
7781
```
7882

7983
```lua
@@ -131,7 +135,8 @@ end
131135
Lua iterators support passing an invisible state object each iteration. If your iterator type does this, you can declare the state type as a second type parameter:
132136

133137
```ts
134-
type MyStateType = ...
138+
type MyStateType =
139+
...
135140
declare function myIterator(): LuaIterable<string, MyStateType>;
136141
```
137142

@@ -155,7 +160,8 @@ Some types can be iterated with `pairs()` (for example, if the `__pairs` method
155160
<SideBySide>
156161

157162
```ts
158-
interface MyType extends LuaPairsIterable<number, string> {}
163+
interface MyType extends LuaPairsIterable<number, string> {
164+
}
159165
declare const obj: MyType;
160166

161167
for (const [key, value] of obj) {
@@ -171,6 +177,28 @@ end
171177

172178
</SideBySide>
173179

180+
Or, if you only care about the key values, you can use `LuaPairsKeyIterable`:
181+
182+
<SideBySide>
183+
184+
```ts
185+
interface MyType extends LuaPairsKeyIterable<number> {
186+
}
187+
declare const obj: MyType;
188+
189+
for (const key of obj) {
190+
console.log(key);
191+
}
192+
```
193+
194+
```lua
195+
for key in pairs(obj) do
196+
print(key)
197+
end
198+
```
199+
200+
</SideBySide>
201+
174202
## Operator Map Types
175203

176204
Lua supports overloading operators on types using [metatable methods](https://www.lua.org/manual/5.4/manual.html#2.4) such as `__add`. But, JavaScript and TypeScript do not support this. In order to use overloaded operators on types that support them, you can declare special mapping functions in TS that will translate to those operators in Lua.
@@ -343,6 +371,14 @@ end
343371

344372
(Remember that in Lua, `pairs()` returns the keys in a random order.)
345373

374+
### `LuaMap` and `LuaSet`
375+
376+
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).
377+
378+
`LuaMap` has the same methods as `LuaTable`, except that `table.get(key)` may also return `undefined`.
379+
380+
`LuaSet`, instead of `table.get/table.set`, has `table.add(value)`, which translates to `table[value] = true`.
381+
346382
### Custom Getters and Setters
347383

348384
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).
@@ -395,9 +431,9 @@ IdDictionary.set(dict, id, "bar");
395431
console.log(IdDictionary.get(dict, id));
396432
```
397433

398-
### All custom LuaTable functions
434+
### All custom Lua table functions
399435

400-
There are more LuaTable functions other than `LuaTableGet` and `LuaTableSet` that you can use:
436+
There are more Lua table functions other than `LuaTableGet` and `LuaTableSet` that you can use:
401437

402438
- `LuaTableGet` - Standalone function that gets a value by key from a table.
403439
- `LuaTableGetMethod` - Method that gets a value by key from the table containing this method.
@@ -407,6 +443,8 @@ There are more LuaTable functions other than `LuaTableGet` and `LuaTableSet` tha
407443
- `LuaTableHasMethod` - Method that checks if a key is present in the table containing this method.
408444
- `LuaTableDelete` - Standalone function that removes a key and its value from a table.
409445
- `LuaTableDeleteMethod` - Method that removes a key and its value from table containing this method.
446+
- `LuaTableAdd` - Standalone function that sets a given key to `true`
447+
- `LuaTableAddMethod` - Method that sets a given key to `true`
410448

411449
## $vararg Constant
412450

0 commit comments

Comments
 (0)