-
Notifications
You must be signed in to change notification settings - Fork 165
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
Clarify behavior of load statement #180
base: master
Are you sure you want to change the base?
Conversation
* loaded variables cannot be bound twice * loaded variables cannot be reassigned * loaded variables are not exported These are the rules from the Java Starlark implementation. Note "loaded variables cannot be reassigned" rule is somewhat inconsistent: loaded variables behave the same as builtins: they are available to the current module but not exported. But builtins can be reassigned. This might need to be changed for greater consistency, but also for practical reasons: reexports. Suppose there's a module which imports a function `foo` and exports it. Currently it can be written like this: ``` load("module.star", _foo="foo") foo = _foo # reexport ``` It is desirable to be able to write this shorter, as: ``` load("module.star", "foo") foo = foo # reexport ``` This resolves issue bazelbuild#37.
When you have If you allow both, how do you resolve load(..., "foo")
def f(): print(foo)
foo = 1 The motivation for the change is to allow If you write
This happens because the range builtin is shadowed (and not visible in the file). The right-hand side of the assignment is a reference to the |
It is inconsistent, but it's also a deliberate choice. Assigning to a global load("...", "foo")
foo = "new_value"
print(foo) # prints the original loaded value of foo, not the new value This is because, just as in Python, the relative location of a use of a name within a block does not change which variable the name refers to. So the loaded The decision to prohibit this kind of name reuse / shadowing does make re-exports uglier. But Deferring this PR until I can revisit the discussion context and chat with Alan. (The reason this isn't a trivial 'accept' is that we don't necessarily want to codify every Java interpreter behavior into the spec, particularly since even the Java interpreter behaves differently in different contexts, like BUILD vs .bzl files.) |
@@ -2903,6 +2903,40 @@ load("module.sky", "x", "y", "z") # assigns x, y, and z | |||
load("module.sky", "x", y2="y", "z") # assigns x, y2, and z | |||
``` | |||
|
|||
Varaibles bound by `load` statement are local to the current module: |
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.
Variables
Loaded variables cannot be rebound, similar to global variables: | ||
* it is a static error to bind a variable more than once with load statements | ||
* it a static or runtime error to bind a global variable if a variable | ||
with the same name name previously loaded |
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.
"name name" => "name was"
|
||
Loaded variables cannot be rebound, similar to global variables: | ||
* it is a static error to bind a variable more than once with load statements | ||
* it a static or runtime error to bind a global variable if a variable |
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 we drop "or runtime"?
These are the rules from the Java Starlark implementation.
Note "loaded variables cannot be reassigned" rule is somewhat
inconsistent: loaded variables behave the same as builtins: they
are available to the current module but not exported.
But builtins can be reassigned.
This might need to be changed for greater consistency, but also for
practical reasons: reexports.
Suppose there's a module which imports a function
foo
and exportsit. Currently it can be written like this:
It is desirable to be able to write this shorter, as:
This resolves issue #37.