Skip to content

Commit 1314ef1

Browse files
authored
added Lua Table types and deprecated @luaTable (#30)
* added Lua Table types and deprecated @luaTable * ran prettier Co-authored-by: Tom <tomblind@users.noreply.github.com>
1 parent 975242d commit 1314ef1

File tree

2 files changed

+140
-25
lines changed

2 files changed

+140
-25
lines changed

docs/advanced/compiler-annotations.md

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -137,30 +137,6 @@ local inst = MyConstructor(3)
137137

138138
</SideBySide>
139139

140-
## @luaTable
141-
142-
**Target elements:** `type`
143-
144-
This annotation signals the transpiler to translate a class as a simple lua table for optimization purposes.
145-
146-
```ts
147-
/** @luaTable */
148-
declare class Table<K extends {} = {}, V = any> {
149-
readonly length: number;
150-
set(key: K, value: V | undefined): void;
151-
get(key: K): V | undefined;
152-
}
153-
154-
const tbl = new Table(); // local tbl = {}
155-
156-
const foo = {};
157-
tbl.set(foo, "bar"); // tbl[foo] = "bar"
158-
print(tbl.get(foo)); // print(tbl[foo])
159-
160-
tbl.set(1, "baz"); // tbl[1] = "baz"
161-
print(tbl.length); // print(#tbl)
162-
```
163-
164140
## @noResolution
165141

166142
**Target elements:** `module`
@@ -751,7 +727,7 @@ for i = 10, 1, -1 do end
751727

752728
### @luaIterator
753729

754-
<DeprecatedInVersion deprecated="0.39.0" removed="TBD" />
730+
<DeprecatedInVersion deprecated="0.38.1" removed="TBD" />
755731

756732
**Target elements:** `(declare) interface`
757733

@@ -834,3 +810,44 @@ for a, b in string.gmatch("foo", "(.)(.)") do end
834810
```
835811

836812
</SideBySide>
813+
814+
### @luaTable
815+
816+
<DeprecatedInVersion deprecated="0.39.0" removed="TBD" />
817+
818+
**Target elements:** `type`
819+
820+
This annotation signals the transpiler to translate a class as a simple lua table for optimization purposes.
821+
822+
```ts
823+
/** @luaTable */
824+
declare class Table<K extends {} = {}, V = any> {
825+
readonly length: number;
826+
set(key: K, value: V | undefined): void;
827+
get(key: K): V | undefined;
828+
}
829+
830+
const tbl = new Table(); // local tbl = {}
831+
832+
const foo = {};
833+
tbl.set(foo, "bar"); // tbl[foo] = "bar"
834+
print(tbl.get(foo)); // print(tbl[foo])
835+
836+
tbl.set(1, "baz"); // tbl[1] = "baz"
837+
print(tbl.length); // print(#tbl)
838+
```
839+
840+
**Upgrade Instructions**
841+
842+
Use the built-in [`LuaTable` language extension](language-extensions.md#lua-table-types) instead of a custom annotated type.
843+
844+
```ts
845+
const tbl = new LuaTable(); // local tbl = {}
846+
847+
const foo = {};
848+
tbl.set(foo, "bar"); // tbl[foo] = "bar"
849+
print(tbl.get(foo)); // print(tbl[foo])
850+
851+
tbl.set(1, "baz"); // tbl[1] = "baz"
852+
print(tbl.length()); // print(#tbl)
853+
```

docs/advanced/language-extensions.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,101 @@ const scaled: Vector = Vector.mul(a, 2);
223223
- LuaBitwiseNot / LuaBitwiseNotMethod (`~x`)
224224
- LuaConcat / LuaConcatMethod (`a .. b`)
225225
- LuaLength / LuaLengthMethod (`#x`)
226+
227+
:::note
228+
You can also map functions to table accessors (`__index` and `__newindex`). See [Lua Table Types](#lua-table-types).
229+
:::
230+
231+
## Lua Table Types
232+
233+
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`.
234+
235+
Example:
236+
237+
<SideBySide>
238+
239+
```ts
240+
const tbl = new LuaTable();
241+
242+
tbl.set("foo", "bar");
243+
console.log(tbl.get("foo"));
244+
245+
const objectKey = {};
246+
tbl.set(objectKey, "baz");
247+
console.log(tbl.get(objectKey));
248+
249+
tbl.set(1, "bah");
250+
console.log(tbl.length());
251+
```
252+
253+
```lua
254+
tbl = {}
255+
256+
tbl.foo = "bar"
257+
print(tbl.foo)
258+
259+
objectKey = {}
260+
tbl[objectKey] = "baz"
261+
print(tbl[objectKey])
262+
263+
tbl[1] = "bah"
264+
print(#tbl)
265+
```
266+
267+
</SideBySide>
268+
269+
`LuaTable` can also be restricted to use only certain types as keys and values:
270+
271+
```ts
272+
const tbl = new LuaTable<KeyType, ValueType>();
273+
```
274+
275+
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).
276+
277+
Example:
278+
279+
<SideBySide>
280+
281+
```ts
282+
interface Id {
283+
idStr: string;
284+
}
285+
286+
interface IdDictionary {
287+
get: LuaTableGetMethod<Id, string>;
288+
set: LuaTableSetMethod<Id, string>;
289+
}
290+
291+
declare const dict: IdDictionary;
292+
const id: Id = { idStr: "foo" };
293+
dict.set(id, "bar");
294+
console.log(dict.get(id));
295+
```
296+
297+
```lua
298+
id = {idStr = "foo"}
299+
dict[id] = "bar"
300+
print(dict[id])
301+
```
302+
303+
</SideBySide>
304+
305+
That example uses the `Method` versions of `LuaTableGet` and `LuaTableSet`. There are also stand-alone versions.
306+
307+
Example:
308+
309+
```ts
310+
declare const idGet: LuaTableGet<IdDictionary, Id, string>;
311+
declare const idSet: LuaTableSet<IdDictionary, Id, string>;
312+
idSet(dict, id, "bar");
313+
console.log(idGet(dict, id));
314+
```
315+
316+
```ts
317+
declare namespace IdDictionary {
318+
export const get: LuaTableGet<IdDictionary, Id, string>;
319+
export const set: LuaTableSet<IdDictionary, Id, string>;
320+
}
321+
IdDictionary.set(dict, id, "bar");
322+
console.log(IdDictionary.get(dict, id));
323+
```

0 commit comments

Comments
 (0)