Skip to content

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 3 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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
Perryvw committed Dec 26, 2020
commit 395fe514f67589f3423f2033c38fedd09f6a882e
60 changes: 60 additions & 0 deletions docs/advanced/language-extensions.md
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`:

This comment was marked as resolved.


```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.


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.

:::

### $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)
```


2 changes: 1 addition & 1 deletion sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"type": "category",
"label": "Advanced",
"items": ["advanced/writing-declarations", "advanced/compiler-annotations"]
"items": ["advanced/writing-declarations", "advanced/compiler-annotations", "advanced/language-extensions"]
},
{
"type": "category",
Expand Down