Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 8 additions & 14 deletions docs/arrow-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,30 @@ apply((x as integer) => x * 2)
apply((x as string) => x + "!")
```

## No closures (yet)
## Closures

Arrow functions do **not** capture variables from the function they are written in. They follow the same scoping rules as regular anonymous functions: they can see parameters, file-level functions, namespaces, classes and globals, but not the locals of the enclosing function. Referencing one is a compile error:
An arrow function that uses a variable from the function it is written in becomes a **closure**: it captures a copy of that variable when it is created, transpiles to an object instead of a plain function, and can only be passed where a `closure(...)` type is declared. See [closures](closures.md) for the details and the rules that come with it.

```brighterscript
sub main()
factor = 2
' error: Arrow functions cannot use variable 'factor' from the enclosing function
scale = (x) => x * factor

' pass it in as a parameter instead
scale = (x, by) => x * by
print scale(10, factor)
scale = (x) => x * factor ' a closure: captures `factor`
print scale(10)
end sub
```

`m` is also not allowed inside an arrow function. Pass the value you need in as an argument instead:
`m` inside an arrow function refers to the `m` of the function it is written in, and using it captures `m` the same way, so an arrow function that uses `m` is also a closure (see [closures](closures.md#m-inside-a-closure)):

```brighterscript
' error: 'm' cannot be used inside an arrow function
' a closure: captures `m`
render = (data) => m.view.setData(data)

' ok
' a plain function: receives the view as an argument instead
render = (data, view) => view.setData(data)
render(data, m.view)
```

Both restrictions are deliberate. Closures are being designed separately (see [#530](https://github.com/rokucommunity/brighterscript/issues/530)) and will most likely reuse the `=>` syntax, so what "capturing a variable" and `m` mean inside an arrow function is left open until then. Code that works today will keep working unchanged once closures exist.

An arrow function that captures nothing and does not use `m` is a plain BrightScript function. It can be passed anywhere a function can: parameters typed `as function` or with a function type, untyped (`dynamic`) parameters, and compiled `.brs` libraries that simply call `callback(x)`.
An arrow function that captures nothing (including `m`) is a plain BrightScript function. It can be passed anywhere a function can: parameters typed `as function` or with a function type, untyped (`dynamic`) parameters, and compiled `.brs` libraries that simply call `callback(x)`.

## Limitations

Expand Down
Loading
Loading