-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add description of local kw in global scope #42794
Conversation
Think it is ready to merge. It just adds an example with explanation regarding the scope to the docs. |
@tomtuamnuq - I am not a JuliaLang member. I do not have rights to approve nor merge PRs here. We need to wait for some of the core developers to review this PR. |
I think that @JeffBezanson needs to review this but it looks ok to me. |
@@ -99,6 +99,19 @@ julia> module E | |||
ERROR: cannot assign variables in other modules | |||
``` | |||
|
|||
Each top-level expression has an invisible scope block around it. If a top-level expression contains a variable declaration with keyword `local`, then that variable is in the local scope of that expression. An example is to declare `local x` in a `begin` or `if` block at the top-level: |
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.
Each top-level expression has an invisible scope block around it.
I think that statement is a bit confusing, since toplevel expressions don't introduce an actual scope block like let
or while
where new assignments are always scoped by default. We should make clear that this only affects variables with an explicit local
declaration.
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.
This is why I don't feel like I can clearly explain what's happening here. Is there a scope or not? If there isn't a scope, then what is the variable local to? Maybe there's a way to explain it, but if so, I don't know how to.
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.
I think I would mostly leave out the word scope here and just say that local
inside top-level expressions means that the variable won't be accessible outside that expression.
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.
@simeonschaub Since the usage of scope seems to be confusing here I removed it.
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.
Thanks! One small tweak, otherwise this looks good to me.
@@ -99,6 +99,22 @@ julia> module E | |||
ERROR: cannot assign variables in other modules | |||
``` | |||
|
|||
If a top-level expression contains a variable declaration with keyword `local`, | |||
then that variable is not accessible outside that expression. | |||
The variable inside the expression does not affect variables of the same name. |
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 variable inside the expression does not affect variables of the same name. | |
The variable inside the expression does not affect global variables of the same name. |
Ah, and it seems like you need to fix the whitespace. |
Doctest hang is likely unrelated, but let's rerun it just to be safe. |
@simeonschaub Thanks for your guidance. tester_linux64 failed again. However, I have not found something about it in open issues. |
That one passed before, I believe that hang is a known issue with CI. Definitely nothing that would be caused by this PR. |
Closes #42664
Adds an explanation of
local
keyword usage in a top-level expression to the Julia Manual.It contains the description @JeffBezanson gave in #10472 and an example as @bkamins gave in #42664.