-
Notifications
You must be signed in to change notification settings - Fork 89
Mutable before return Export-by-Value Semantics #179
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
Open
MagmaBurnsV
wants to merge
4
commits into
luau-lang:master
Choose a base branch
from
MagmaBurnsV:Alternative-Exports-RFC
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+253
−0
Open
Changes from all commits
Commits
Show all changes
4 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
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,253 @@ | ||
| # Export by Value | ||
|
|
||
| ## Summary | ||
| Extend the `export` keyword to support variables and functions as syntax sugar for constructing a table and returning it from a module. | ||
|
|
||
| ## Motivation | ||
| Today, type aliases are able to be exported from a module using the `export` keyword. | ||
|
|
||
| ```luau | ||
| export type Point = {x: number, y: number} | ||
| ``` | ||
|
|
||
| However, this mechanism is currently only supported by types. | ||
|
|
||
| Extending `export` to also support variable and function declarations would provide a consistent way for users to expose a stable API from modules. | ||
|
|
||
| Furthermore, introducing a language-level concept of exporting opens the door to many optimizations not currently possible with dynamic module returns, such as cross-module inlining and constant-folding. | ||
|
|
||
| ## Design | ||
| The `export` contextual keyword will now be allowed anywhere before variable and function declarations at the top level of a module, including `local`, `const` and `function` declarations. | ||
|
|
||
| Exporting declarations that are in nested `do end` blocks under the top scope are also permitted. Attempting to export a declaration outside the module scope will result in a parse error. | ||
|
|
||
| Exported variables will count towards the local variable limit, as they can be optimized into locals by the compiler. | ||
|
|
||
| ```luau | ||
| export local version = "5.1" | ||
| export const TAU = math.pi * 2 | ||
|
|
||
| export function init() -- exported functions are always const | ||
| -- TODO | ||
| end | ||
|
|
||
| do | ||
| local counter = 0 | ||
|
|
||
| export function increment(): number | ||
| counter += 1 | ||
| return counter | ||
| end | ||
| end | ||
| -- counter and increment not visible here | ||
|
|
||
| if foo then | ||
| export local bar = 1 -- not allowed, syntax error | ||
| end | ||
| ``` | ||
|
|
||
| Just like normal variable declarations, it is possible to export multiple variables, variables with type annotations, uninitialized variables, and const variables. | ||
|
|
||
| ```luau | ||
| export local settings: Settings = getSettings() | ||
| export local a, b, c = 1, 2, 3 | ||
| export local d -- same as export d = nil | ||
| export const MAX_ITEMS = 10 -- cannot be reassigned | ||
| ``` | ||
|
|
||
| Exporting a variable with the same identifier twice is a parse error, regardless of the scope it was defined in. | ||
|
|
||
| ```luau | ||
| export local foo = 1 | ||
| do | ||
| export local foo = 2 -- syntax error | ||
| end | ||
| ``` | ||
|
|
||
| However, exporting a variable with the same identifier as another non-exported variable is allowed, following conventional lexical scoping and shadowing rules. | ||
|
|
||
| ```luau | ||
| local function foo() return 1 end | ||
| export function foo() return 2 end -- exported, shadows foo | ||
|
|
||
| print(foo()) -- 2 | ||
|
|
||
| local fruit = "apple" | ||
| export local fruit -- exports fruit = nil, not "apple" | ||
| print(fruit) -- nil | ||
|
|
||
| export local animal = "dog" | ||
| local animal = "cat" -- shadows animal, doesn't change export | ||
| animal = "bird" | ||
|
|
||
| print(animal) -- bird | ||
| ``` | ||
|
|
||
| Exported function declarations are also always treated as const, as reassigning function declarations is usually a mistake. | ||
|
|
||
| ```luau | ||
| export function f() end | ||
| f = 1 -- illegal | ||
| ``` | ||
|
|
||
| ### Desugared Form | ||
| Exporting variables desugars into assigning keys to a table that is then frozen and returned once the module scope ends. | ||
|
|
||
| ```luau | ||
| export local a = 1 | ||
|
|
||
| -- desugars into | ||
|
|
||
| local _EXP = {} | ||
| _EXP.a = 1 | ||
| return table.freeze(_EXP) | ||
| ``` | ||
|
|
||
| Exported local variables can therefore be reassigned within the module after being declared. This allows for conditional exports and forward declarations. | ||
|
|
||
| ```luau | ||
| export local side = "heads" | ||
| if math.random(0, 1) == 1 then | ||
| side = "tails" | ||
| end | ||
|
|
||
| export local f, g | ||
| function f() | ||
| g() | ||
| end | ||
| function g() | ||
| f() | ||
| end | ||
|
|
||
| -- desugars into | ||
|
|
||
| local _EXP = {} | ||
| _EXP.side = "heads" | ||
| if math.random(0, 1) == 1 then | ||
| _EXP.side = "tails" | ||
| end | ||
|
|
||
| _EXP.f, _EXP.g = nil, nil | ||
| function _EXP.f() | ||
| _EXP.g() | ||
| end | ||
| function _EXP.g() | ||
| _EXP.f() | ||
| end | ||
|
|
||
| return table.freeze(_EXP) | ||
| ``` | ||
|
|
||
| Once the module ends and the export table is frozen, subsequent reassignments will throw a runtime error, analogous to reassigning keys to a frozen table. | ||
|
|
||
| ```luau | ||
| export local counter = 0 | ||
|
|
||
| export function increment() | ||
| -- once the module returns | ||
| -- this raises an "attempt to modify a readonly table" error | ||
| counter += 1 | ||
| end | ||
|
|
||
| -- desugars into | ||
|
|
||
| local _EXP = {} | ||
| _EXP.counter = 0 | ||
|
|
||
| function _EXP.increment() | ||
| _EXP.counter += 1 | ||
| end | ||
|
|
||
| return table.freeze(_EXP) | ||
| ``` | ||
|
|
||
| ### Typechecking | ||
| Type inference of exported variables will behave exactly the same as local variable inference. | ||
|
|
||
| ```luau | ||
| -- example behavior subject to change with local inference changes | ||
|
|
||
| export local foo = 15 | ||
| foo = "hello" | ||
| foo = true | ||
| -- foo: number | string | boolean | ||
|
|
||
| export local bar = nil | ||
| if math.random(0, 1) == 1 then | ||
| bar = 123 | ||
| end | ||
| -- bar: number? | ||
| ``` | ||
|
|
||
| This is in contrast to how typechecking would behave in the desugared form with key assignments. | ||
|
|
||
| ```luau | ||
| local _EXP = {} | ||
| _EXP.foo = 15 | ||
| _EXP.foo = "hello" -- type error | ||
| _EXP.foo = true -- type error | ||
|
|
||
| _EXP.bar = nil | ||
| if math.random(0, 1) == 1 then | ||
| _EXP.bar = 123 -- type error | ||
| end | ||
|
|
||
| return table.freeze(_EXP) | ||
| ``` | ||
|
|
||
| This is done to ensure exported variables act exactly the same as normal variables and preserve the same ergonomics. | ||
|
|
||
| ### Interaction with `return` | ||
|
|
||
| A module that contains an export statement is not permitted to also contain a return statement at the module scope, as the two mechanisms are mutually exclusive. If there is both an export statement and return statement, it is a parse error. | ||
|
|
||
| ```luau | ||
| export local a = 1 | ||
| return {b = 2} -- syntax error | ||
| ``` | ||
| ```luau | ||
| if skip then return end | ||
| export local a = 1 -- syntax error | ||
| ``` | ||
|
|
||
| This restriction does not apply to modules that only contain type exports for backwards compatibility. | ||
|
|
||
| ### Future Optimizations | ||
|
|
||
| While the primary purpose for extending exports is user ergonomics, first-class exports also open the door to many optimizations that aren't currently possible with dynamic module returns. | ||
|
|
||
| For example, exported variables and functions can be transformed into local variables that are stored in VM registers for faster lookup: | ||
|
|
||
| ```luau | ||
| export const TAU = math.pi * 2 | ||
| print(TAU) | ||
|
|
||
| -- becomes | ||
| const TAU = math.pi * 2 | ||
| print(TAU) | ||
| return table.freeze({TAU = TAU}) | ||
| ``` | ||
|
|
||
| Furthermore, first-class exports allow for the compiler to assume exported variables are never reassigned after module return, unlocking the ability to bring constant-folding and inlining across module boundaries. | ||
|
|
||
| ## Drawbacks | ||
|
|
||
| This increases compiler complexity in terms of tracking exported declarations and converting them into table assignments. | ||
|
|
||
| Allowing reassignment of exported keys after module return isn't possible. This is a deliberate design choice intended to make exports easier to optimize. | ||
|
|
||
| The shadowing rules for exported variables with relation to non-exported variables may lead to confusion. Similarly, reassigning exported variables in a large file may be as equally confusing. Users are encouraged to declare their exported variables with const if they do not intend to reassign them in the module. | ||
|
|
||
| Uninitialized exported variables pose a footgun where one can declare an uninitialized variable that is never reassigned to. This can be solved with linting. | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One additional drawback is, in large Luau files, reassignable exports can introduce a potential footgun. That is the tradeoff to enable cleaner mutual recursion, but I think it should be mentioned at least. |
||
| ## Alternatives | ||
|
|
||
| As always, do nothing and leave users to construct their own module return tables. This would forfeit providing a consistent way for users to expose public APIs and would not allow for future cross-module optimizations. | ||
|
|
||
| Permitting exports in `do end` blocks can be scrapped, but this would prevent users from scoping exported declarations and doesn't make much sense not to support. It is also possible to initially not support this and then add it back in later. | ||
|
|
||
| An earlier version of the exported variable syntax omitted declaration keywords, such as with `export x = 1`. This was changed in lieu of the addition of const. | ||
|
|
||
| Exported functions may instead not be automatically const, or declaration keywords may be allowed before exported functions such as with `export const function f() end`. Neither of these options are done since reassigning function declarations is already discouraged practice, and adding more keywords between function exports makes it unnecessarily verbose. | ||
|
|
||
| All exported variables could be treated as const with applicable syntax changes. However, this makes many useful patterns such as exporting mutually dependent functions hard to pull off. Furthermore, introducing categorical const-ness would not allow for any more optimization opportunities. | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I have an example where you would want to hide exported values from the rest of the module they are defined in? I'm struggling to see why this would be desired behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first codeblock in the doc shows an example where an exported function references a scoped upvalue for encapsulation.
It's less about hiding the exported bindings and more about hiding their dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hiding the dependencies makes sense, however, I do think it will be nicely covered by the classes RFC, plus any additional RFC extensions to add a
privatekeyword.