diff --git a/docs/advanced/compiler-annotations.md b/docs/advanced/compiler-annotations.md index 67be2450..a8c4c2d4 100644 --- a/docs/advanced/compiler-annotations.md +++ b/docs/advanced/compiler-annotations.md @@ -137,34 +137,6 @@ local inst = MyConstructor(3) -## @forRange - -**Target elements:** `declare function` - -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. - -The function should not be a real function and an error will be thrown if it is used in any other way. - -**Example** - - - - -```typescript -/** @forRange */ -declare function forRange(start: number, limit: number, step?: number): number[]; - -for (const i of forRange(1, 10)) {} -for (const i of forRange(10, 1, -1)) {} -``` - -```lua -for i = 1, 10 do end -for i = 10, 1, -1 do end -``` - - - ## @luaIterator **Target elements:** `(declare) interface` @@ -776,3 +748,50 @@ MyClass = __TS__Class() ``` + +## @forRange + +**Target elements:** `declare function` + +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. + +The function should not be a real function and an error will be thrown if it is used in any other way. + +**Example** + + + + +```typescript +/** @forRange */ +declare function forRange(start: number, limit: number, step?: number): number[]; + +for (const i of forRange(1, 10)) {} +for (const i of forRange(10, 1, -1)) {} +``` + +```lua +for i = 1, 10 do end +for i = 10, 1, -1 do end +``` + + + +**Upgrade Instructions** + +Use the [`$range` language extension](language-extensions.md#$range-iterator-function) instead of a custom annotated type. + + + + +```typescript +for (const i of $range(1, 10)) {} +for (const i of $range(10, 1, -1)) {} +``` + +```lua +for i = 1, 10 do end +for i = 10, 1, -1 do end +``` + + diff --git a/docs/advanced/language-extensions.md b/docs/advanced/language-extensions.md index 2f4194f1..1a7b8434 100644 --- a/docs/advanced/language-extensions.md +++ b/docs/advanced/language-extensions.md @@ -61,6 +61,29 @@ end foo, four = myFunc(nil) ``` +## $range Iterator Function + +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. + +Example: + + + + +```ts +for (const i of $range(1, 5)) {} +for (const i of $range(1, 10, 2)) {} +for (const i of $range(5, 1, -1)) {} +``` + +```lua +for i = 1, 5 do end +for i = 1, 10, 2 do end +for i = 5, 1, -1 do end +``` + + + ## Operator Map Types 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.