Reputation: 873
Consider this source code
println("Julia language version ",VERSION)
i=666
for i = 1:2
println("i is $i")
end
println("global i is $i")
function main()
j = 666
for j = 1:2
println("j is $j")
end
println("global j is $j")
end
main()
Consider the output of version 0.6
Julia language version 0.6.3
i is 1
i is 2
global i is 2
j is 1
j is 2
global j is 2
Compare to the output of version 1.0
Julia language version 1.0.0
i is 1
i is 2
global i is 666
j is 1
j is 2
global j is 666
I can't change the value of variable i and variable j using a for loop like I can before in version 0.6
I think C programmers will have the shock of their lives...
Upvotes: 1
Views: 222
Reputation: 871
If you use Julia 0.7 (which is basically == 1.0 with deprecations) you would see the necessary deprecation messages for the intended behaviour change:
┌ Warning: Deprecated syntax `implicit assignment to global variable `i``.
│ Use `global i` instead.
└ @ none:0
┌ Warning: Loop variable `i` overwrites a variable in an enclosing scope. In the future the variable will be local to the loop instead.
└ @ none:0
i is 1
i is 2
global i is 2
So to get what you want you could write:
function main()
global j = 666
for j = 1:2
println("j is $j")
end
println("global j is $j")
end
main()
Your first example on the global level should theoretically be dealt with using for outer i..
as described in https://docs.julialang.org/en/latest/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1 but currently this is not handled in the REPL. See this issue: https://github.com/JuliaLang/julia/issues/24293
Upvotes: 4