Skip to content

Prototype closures: arrow functions that capture enclosing variables … - #1933

Draft
iObject wants to merge 3 commits into
1929-arrow-function-supportfrom
530-add-closure-support
Draft

iObject wants to merge 3 commits into
1929-arrow-function-supportfrom
530-add-closure-support

Conversation

@iObject

@iObject iObject commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

#530

An arrow function that references a local or parameter of its enclosing function becomes a closure. Captured values are copied when the closure is created and it transpiles to { ctx: {...}, fn: function(__bsc_ctx, ...) } with the values unpacked into locals so the body is emitted unchanged. Calls on statically-known closures become f.fn(f.ctx, ...).

Adds the contextual closure(...) type. Closures never widen to function, dynamic or object (closure-not-compatible-with-parameter), and reassigning a captured variable after the closure is an error (closure-captured-variable-reassigned). Non-capturing arrow functions are unchanged plain functions.

…530)

An arrow function that references a local or parameter of its enclosing
function becomes a closure. Captured values are copied when the closure is
created and it transpiles to `{ ctx: {...}, fn: function(__bsc_ctx, ...) }`
with the values unpacked into locals so the body is emitted unchanged.
Calls on statically-known closures become `f.fn(f.ctx, ...)`.

Adds the contextual `closure(...)` type. Closures never widen to
`function`, `dynamic` or `object` (closure-not-compatible-with-parameter),
and reassigning a captured variable after the closure is an error
(closure-captured-variable-reassigned). Non-capturing arrow functions are
unchanged plain functions.
…ts (#530)

Drop each arrow function's capture memo from CachedLookups.invalidate()
instead of a separate reset step in BrsFileValidator, so a plugin's AST
changes are picked up together with the other lookups.

Assert exact ranges for the closure-captured-variable-reassigned,
closure-not-compatible-with-parameter and arrow-function-cannot-use-m
diagnostics. Label every before/after transpile pair in docs/closures.md
and open it with worked examples: composing closures, a typed array of
rule closures, and memoizing through a captured cache.
An arrow function that uses `m` now sees the `m` of the function it is
written in and captures it, becoming a closure, the way an arrow function
in JavaScript sees the enclosing `this`. Since arrow functions resolve
`m` through the enclosing function's symbol chain, `typecast m` types
and class instance types apply inside the closure.

Removes the arrow-function-cannot-use-m diagnostic and its validation,
converts the tests to cover capture, typing and the nested-function case,
and documents the behavior with transpiled output.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant