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

Add description of local kw in global scope #42794

Merged
merged 5 commits into from
Jan 14, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/src/manual/variables-and-scoping.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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.


```jldoctest
julia> x = 1
begin
local x = 0
@show x
end
@show x;
x = 0
x = 1
```

Note that the interactive prompt (aka REPL) is in the global scope of the module `Main`.

## Local Scope
Expand Down