-
Notifications
You must be signed in to change notification settings - Fork 22
Added language extensions page #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Added language extensions page
- Loading branch information
commit 395fe514f67589f3423f2033c38fedd09f6a882e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
title: Language extensions | ||
--- | ||
|
||
import { SideBySide } from "@site/src/components/SideBySide"; | ||
|
||
TypeScriptToLua provides several extensions to typescript in the form of types and helper functions. To use these language extensions, add the types to your `tsconfig.json`: | ||
|
||
```json | ||
{ | ||
"compilerOptions": { | ||
... | ||
"types": ["typescript-to-lua/language-extensions"], | ||
... | ||
}, | ||
} | ||
``` | ||
|
||
## MultiReturn Type | ||
This language extension allows typing of Lua functions that return multiple values. For example, consider Lua's `string.find`, it returns two indices: the start of the found substring and the end of the found substring. In TypeScript, function can only return one value so a special type is needed to indicate to tstl there are multiple return values. This is the `MultiReturn<>` type. | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
It allows us to declare `string.find` like this: | ||
|
||
|
||
```ts title=stringfind.ts | ||
declare namespace string { | ||
export function find(haystack: string, needle: string): MultiReturn<[number, number]>; | ||
} | ||
|
||
const [start, end] = string.find("Hello, world!", "world"); | ||
``` | ||
Translating into: | ||
```lua title=stringfind.lua | ||
start, ____end = string.find("Hello, world!", "world") | ||
``` | ||
|
||
:::note | ||
Prefer MultiReturn over the similar [@tupleReturn anotation](./compiler-annotations.md#tuplereturn). MultiReturn 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. | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
::: | ||
|
||
### $multi | ||
Now that we have a type-safe way of describing multiple return values of a function, we would also like a type-safe way of creating instances of this type. In order to create a function that returns multiple values it needs to return a `MultiReturn<>` type. This is where the `$multi` function comes in. Calling `$multi` in a return statement will create an instance of the `MultiReturn<>` type: | ||
|
||
```ts title=multi.ts | ||
function myFunc(): MultiReturn<[string, number]> { | ||
return $multi("foo", 4); | ||
} | ||
|
||
const [foo, four] = myFunc(); | ||
``` | ||
|
||
Translates into: | ||
```lua title=multi.lua | ||
function myFunc(self) | ||
return "foo", 4 | ||
end | ||
foo, four = myFunc(nil) | ||
``` | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.