Skip to content
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

Make documentation clear about handling of local in global scope #42664

Closed
bkamins opened this issue Oct 15, 2021 · 4 comments · Fixed by #42794
Closed

Make documentation clear about handling of local in global scope #42664

bkamins opened this issue Oct 15, 2021 · 4 comments · Fixed by #42794
Labels
docs This change adds or pertains to documentation

Comments

@bkamins
Copy link
Member

bkamins commented Oct 15, 2021

Following https://stackoverflow.com/questions/69587809/how-to-think-of-begin-block-in-julia and discussion on Slack it would be ideal to make a clear explanation in the Julia manual how the following code blocks:

x = 1
local x = 0
@show x

and

x = 1
begin
    local x = 0
    @show x
    x += 1
end
@show x

and

x = 1
if true
    local x = 0
    @show x
    x += 1
end
@show x

are handled if they are in global scope in two modes:

  1. run in REPL
  2. run from script

CC @JeffBezanson

@JeffBezanson
Copy link
Member

See #10472

The answer is that each top-level expression has an invisible scope block around it that local uses. I'm pretty sure you can only detect the presence of this invisible scope with local declarations; otherwise it shouldn't affect anything. This should be the same in a script and the REPL.

Historically, this was useful for hygienic macros, which can freely add new local variables without either (1) introducing an explicit scope block like a let or (2) leaking variables into the global namespace, when used at the top level.

@JeffBezanson JeffBezanson added the docs This change adds or pertains to documentation label Oct 15, 2021
@tomtuamnuq
Copy link
Contributor

We could add something at the end of Global-Scope Docs to describe the behavior of local keyword in global scope.
The docs explicitly state "Notably missing from this table are begin blocks and if blocks which do not introduce new scopes. " For me this causes some confusion.
I would therefore add a sentence to the description of global scope to state that begin and if blocks end the local scope after using local, because "if a top-level expression contains local x = ..., then that expression has its own local scope" ( as @JeffBezanson stated in 10472 )

I have tested the code blocks in interactive mode and from script and it is the same. Since the docs already state
"Note that the interactive prompt (aka REPL) is in the global scope of the module Main." for me it seems clear.

@kitarp29
Copy link

@JeffBezanson Could you assign this issue to me?

@KristofferC
Copy link
Member

We typically don't assign issues to persons unless there is a strong reason to do so (e.g if the person is the original author of the code that needs fixing). If you want to work on this, you can just open a PR, no need to get assigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants