-
Notifications
You must be signed in to change notification settings - Fork 60
Import syntax #103
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
jackdotink
wants to merge
3
commits into
luau-lang:master
Choose a base branch
from
jackdotink:import-syntax
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.
Open
Import syntax #103
Changes from 1 commit
Commits
Show all changes
3 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,70 @@ | ||
# Import Syntax | ||
|
||
## Summary | ||
|
||
This proposal introduces new syntax to import exported values and types from modules. This syntax pairs well with the existing `export type name = type` syntax and the proposed `export name = value` syntax. In a simple example: `import name, name2, type type1, type type2 from "module"`. | ||
|
||
## Motivation | ||
|
||
The last RFC to propose import syntax was rejected with the primary reason being that destructuring solved all of the same problems, and was a more expansive feature. At this point in time destructuring is pretty much dead in the water, with a destructuring RFC being rejected, and current RFC(s) in limbo. | ||
|
||
The proposed import syntax enables programmers to control precisely what they bring into scope from a module. This is particularly useful with larger libraries, like react. | ||
|
||
```luau | ||
-- what was once this: | ||
local React = require("@React") | ||
local createElement, useState, useEffect, useContext, useRef = React.createElement, React.useState, React.useEffect, React.useContext, React.useRef | ||
-- can now be this: | ||
import createElement, useState, useEffect, useContext, useRef from "@React" | ||
``` | ||
|
||
## Design | ||
|
||
In EBNF: | ||
|
||
```ebnf | ||
chunk = imports block | ||
|
||
imports = {import [';']} | ||
import = 'import' {importname ','} [importname] 'from' STRING | ||
importname = ['type'] NAME | ||
``` | ||
|
||
Imports would only exist at the top of files, shebang and comments are ignored, and so would be allowed above and inside of imports. | ||
|
||
This import: | ||
|
||
```luau | ||
import createElement, useState, useEffect, useContext, useRef from "@React" | ||
``` | ||
|
||
Would be equivalent to: | ||
|
||
```luau | ||
local React = require("@React") | ||
local createElement, useState, useEffect, useContext, useRef = React.createElement, React.useState, React.useEffect, React.useContext, React.useRef | ||
``` | ||
|
||
Types could also be imported, which was previously impossible: | ||
|
||
```luau | ||
import myvalue1, type mytype1, type mytype2 from "MyModule" | ||
``` | ||
|
||
At runtime, type imports would be ignored. | ||
|
||
The string after the `from` contextual keyword would be a string literal, and would follow the same rules for module resolution as the `require` function. | ||
|
||
## Drawbacks | ||
|
||
This gives users a second way to import modules, which could be confusing to new users and could lead to inconsistent codebases. This would already be the case with the proposed export-value syntax. | ||
|
||
## Alternatives | ||
|
||
The primary alternative is to continue using the existing `require` function. | ||
|
||
Some smaller alternatives are: | ||
|
||
* Change the contextual keyword `from` to the existing reserved keyword `in`. This may reduce parsing complexity, but would be less intuitive. | ||
* Allow programmers to rebind imported values to other names, perhaps something like `import createElement as e from "@React"`. This would be more flexible, but would also be more complex. This pattern could also not be done with autocomplete, as the autocomplete would not know the new name. It is also a fairly uncommon pattern, existing in usage of only a few libraries. Those users could continue to rebind with a `local` statement. | ||
* Figure out destructuring syntax, and use that instead. This would be a more expansive feature, and would be more powerful, but would also be more complex. This would also be a more common pattern, and would be more intuitive to users coming from other languages. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.