Skip to content

Commit 68dcd13

Browse files
authored
added $range language extension and moved forRange annotation to deprecated section (#25)
Co-authored-by: Tom <tomblind@users.noreply.github.com>
1 parent 475a612 commit 68dcd13

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

docs/advanced/compiler-annotations.md

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

138138
</SideBySide>
139139

140-
## @forRange
141-
142-
**Target elements:** `declare function`
143-
144-
Denotes a function declaration is a Lua numerical iterator. When used in a TypeScript `for...of` loop, the resulting Lua will use a numerical for loop.
145-
146-
The function should not be a real function and an error will be thrown if it is used in any other way.
147-
148-
**Example**
149-
150-
<SideBySide>
151-
152-
<!-- prettier-ignore -->
153-
```typescript
154-
/** @forRange */
155-
declare function forRange(start: number, limit: number, step?: number): number[];
156-
157-
for (const i of forRange(1, 10)) {}
158-
for (const i of forRange(10, 1, -1)) {}
159-
```
160-
161-
```lua
162-
for i = 1, 10 do end
163-
for i = 10, 1, -1 do end
164-
```
165-
166-
</SideBySide>
167-
168140
## @luaIterator
169141

170142
**Target elements:** `(declare) interface`
@@ -776,3 +748,50 @@ MyClass = __TS__Class()
776748
```
777749

778750
</SideBySide>
751+
752+
## @forRange
753+
754+
**Target elements:** `declare function`
755+
756+
Denotes a function declaration is a Lua numerical iterator. When used in a TypeScript `for...of` loop, the resulting Lua will use a numerical for loop.
757+
758+
The function should not be a real function and an error will be thrown if it is used in any other way.
759+
760+
**Example**
761+
762+
<SideBySide>
763+
764+
<!-- prettier-ignore -->
765+
```typescript
766+
/** @forRange */
767+
declare function forRange(start: number, limit: number, step?: number): number[];
768+
769+
for (const i of forRange(1, 10)) {}
770+
for (const i of forRange(10, 1, -1)) {}
771+
```
772+
773+
```lua
774+
for i = 1, 10 do end
775+
for i = 10, 1, -1 do end
776+
```
777+
778+
</SideBySide>
779+
780+
**Upgrade Instructions**
781+
782+
Use the [`$range` language extension](language-extensions.md#$range-iterator-function) instead of a custom annotated type.
783+
784+
<SideBySide>
785+
786+
<!-- prettier-ignore -->
787+
```typescript
788+
for (const i of $range(1, 10)) {}
789+
for (const i of $range(10, 1, -1)) {}
790+
```
791+
792+
```lua
793+
for i = 1, 10 do end
794+
for i = 10, 1, -1 do end
795+
```
796+
797+
</SideBySide>

docs/advanced/language-extensions.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ end
6161
foo, four = myFunc(nil)
6262
```
6363

64+
## $range Iterator Function
65+
66+
Typescript's numeric for loops are less restrictive than Lua's, so they are transpiled into while loops instead. To create a Lua-style numeric for loop, you can use the `$range` language extension in a for...of loop.
67+
68+
Example:
69+
70+
<SideBySide>
71+
72+
<!-- prettier-ignore -->
73+
```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)) {}
77+
```
78+
79+
```lua
80+
for i = 1, 5 do end
81+
for i = 1, 10, 2 do end
82+
for i = 5, 1, -1 do end
83+
```
84+
85+
</SideBySide>
86+
6487
## Operator Map Types
6588

6689
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

Comments
 (0)