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/compiler-annotations.md
+86-49Lines changed: 86 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,55 +137,6 @@ local inst = MyConstructor(3)
137
137
138
138
</SideBySide>
139
139
140
-
## @luaIterator
141
-
142
-
**Target elements:**`(declare) interface`
143
-
144
-
Denotes a type is a Lua iterator. When an object of a type with this annotation is used in a for...of statement, it will transpile directly as a lua iterator in a for...in statement, instead of being treated as a TypeScript iterable. Typically, this is used on an interface that extends `Iterable` or `Array` so that TypeScript will allow it to be used in a for...of statement.
145
-
146
-
**Example**
147
-
148
-
<SideBySide>
149
-
150
-
<!-- prettier-ignore -->
151
-
```typescript
152
-
/**@luaIterator*/
153
-
typeLuaIterable<T> =Iterable<T>;
154
-
155
-
declarefunction myIterator():LuaIterable<string>;
156
-
for (const s ofmyIterator()) {}
157
-
```
158
-
159
-
```lua
160
-
forsinmyIterator() doend
161
-
```
162
-
163
-
</SideBySide>
164
-
165
-
This can also be combined with [@tupleReturn](#tuplereturn), if the iterator returns multiple values.
166
-
167
-
**Example**
168
-
169
-
<SideBySide>
170
-
171
-
<!-- prettier-ignore -->
172
-
```typescript
173
-
/**@luaIterator@tupleReturn*/
174
-
typeLuaTupleIterable<Textendsany[]> =Iterable<T>;
175
-
176
-
declarenamespacestring {
177
-
function gmatch(s:string, pattern:string):LuaTupleIterable<string[]>;
178
-
}
179
-
180
-
for (const [a, b] ofstring.gmatch("foo", "(.)(.)")) {}
Denotes a type is a Lua iterator. When an object of a type with this annotation is used in a for...of statement, it will transpile directly as a lua iterator in a for...in statement, instead of being treated as a TypeScript iterable. Typically, this is used on an interface that extends `Iterable` or `Array` so that TypeScript will allow it to be used in a for...of statement.
759
+
760
+
**Example**
761
+
762
+
<SideBySide>
763
+
764
+
<!-- prettier-ignore -->
765
+
```typescript
766
+
/**@luaIterator*/
767
+
typeLuaIterator<T> =Iterable<T>;
768
+
769
+
declarefunction myIterator():LuaIterator<string>;
770
+
for (const s ofmyIterator()) {}
771
+
```
772
+
773
+
```lua
774
+
forsinmyIterator() doend
775
+
```
776
+
777
+
</SideBySide>
778
+
779
+
This can also be combined with [@tupleReturn](#tuplereturn), if the iterator returns multiple values.
780
+
781
+
**Example**
782
+
783
+
<SideBySide>
784
+
785
+
<!-- prettier-ignore -->
786
+
```typescript
787
+
/**@luaIterator@tupleReturn*/
788
+
typeLuaTupleIterator<Textendsany[]> =Iterable<T>;
789
+
790
+
declarenamespacestring {
791
+
function gmatch(s:string, pattern:string):LuaTupleIterator<string[]>;
792
+
}
793
+
794
+
for (const [a, b] ofstring.gmatch("foo", "(.)(.)")) {}
795
+
```
796
+
797
+
```lua
798
+
fora, binstring.gmatch("foo", "(.)(.)") doend
799
+
```
800
+
801
+
</SideBySide>
802
+
803
+
**Upgrade Instructions**
804
+
805
+
Use the [`LuaIterable` and `LuaMultiReturn` language extensions](language-extensions.md#luaiterable-type) instead of a custom annotated types.
806
+
807
+
<SideBySide>
808
+
809
+
<!-- prettier-ignore -->
810
+
```typescript
811
+
declarefunction myIterator():LuaIterable<string>;
812
+
for (const s ofmyIterator()) {}
813
+
```
814
+
815
+
```lua
816
+
forsinmyIterator() doend
817
+
```
818
+
819
+
</SideBySide>
820
+
821
+
<SideBySide>
822
+
823
+
<!-- prettier-ignore -->
824
+
```typescript
825
+
declarenamespacestring {
826
+
function gmatch(s:string, pattern:string):LuaIterable<LuaMultiReturn<string[]>>;
827
+
}
828
+
829
+
for (const [a, b] ofstring.gmatch("foo", "(.)(.)")) {}
Prefer LuaMultiReturn over the similar [@tupleReturn annotation](./compiler-annotations.md#tuplereturn). LuaMultiReturn can do anything tupleReturn can, with the added benefit of being able to distinguish between actual tuple tables and multiple return values in the type system.
41
41
:::
42
42
43
-
### \$multi
43
+
### $multi
44
44
45
45
In order to create a function that returns multiple values it needs to return a `LuaMultiReturn<>` type. This is where the `$multi` function comes in. Calling `$multi` in a return statement will create an instance of the `LuaMultiReturn<>` type:
46
46
@@ -84,6 +84,70 @@ for i = 5, 1, -1 do end
84
84
85
85
</SideBySide>
86
86
87
+
## LuaIterable Type
88
+
89
+
Iterators in Lua work quite differently than in Typescript/Javscript, so a special type is needed to use them.
90
+
91
+
For example, to declare and use a Lua function that returns an iterator for a set of strings, you can do this:
92
+
93
+
<SideBySide>
94
+
95
+
```ts
96
+
declarefunction myIterator():LuaIterable<string>;
97
+
98
+
for (const s ofmyIterator()) {
99
+
console.log(s);
100
+
}
101
+
```
102
+
103
+
```lua
104
+
forsinmyIterator() do
105
+
print(s)
106
+
end
107
+
```
108
+
109
+
</SideBySide>
110
+
111
+
Some iterators return multiple values each iteration. To declare these, combine `LuaIterable` with [`LuaMultiReturn`](#luamultireturn-type):
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:
This is only really required if you need to use the iterator outside of a `for...of` loop.
139
+
140
+
```ts
141
+
let [iteratorFunction, state, lastValue] =myIterator();
142
+
while (true) {
143
+
const value =iteratorFunction(state, lastValue);
144
+
console.log(value);
145
+
lastValue=value;
146
+
}
147
+
```
148
+
149
+
See the [Lua Reference Manual](https://www.lua.org/manual/5.3/manual.html#3.3.5) for more information on Lua for loops.
150
+
87
151
## Operator Map Types
88
152
89
153
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.
0 commit comments