7 ms·
Julia started with a single scoping algorithm. In v1, they got two (toplevel and function-level). Now, Julia has three distinct scoping algorithms. See the th
by espadrine 6y ago
Julia started with a single scoping algorithm. In v1, they got two (toplevel and function-level).
Now, Julia has three distinct scoping algorithms.
See the three distinct behavior for the same two pieces of code:
Program Ⓐ:
l = 0
for i = 1:10
l = i
end
println(l)
Program Ⓑ:
local l = 0
for i = 1:10
l = i
end
println(l)
• REPL: Program Ⓐ gives “10”, Program Ⓑ gives “UndefVarError: l not defined”.
• TOPLEVEL: Program Ⓐ gives “0”, Program Ⓑ gives “UndefVarError: l not defined”.
• FUNCTION: Program Ⓐ gives “10”, Program Ⓑ gives “10”.
- manwe150 6y agoI think you misunderstood the v0.x scoping algorithm. In v0.x, there were 3 scoping situations described in the documentation (function, toplevel, and outer). In v1.0, there was essentially 1 (plus clarifications on what a variable was "local" to in various, generally toplevel, situations). Now there are 2, but TOPLEVEL also emits the following warning, that you didn't mention, since it sees the behavior diverges between them: Warning: Assignment to `l` in soft scope is ambiguous because a global variable by the same name exists: `l` will be treated as a new local. Disambiguate by using `local l` to suppress this warning or `global l` to assign to the existing global variable. And while you write "same piece of code", the code you then paste is not the same: the statement "local" appears in a different statement block, leading to it being executed in a subsequent scope block.