Repository navigation
Conversation
…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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
#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 becomef.fn(f.ctx, ...).Adds the contextual
closure(...)type. Closures never widen tofunction,dynamicorobject(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.