Julia for循环中变量的行为方式已更改

Julia for循环中变量的行为方式已更改,julia,Julia,考虑一下这个源代码 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() 考虑

考虑一下这个源代码

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() 
考虑版本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
与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
我不能像以前在0.6版中那样使用for循环来更改变量I和变量j的值

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
我认为C程序员会对他们的生活感到震惊…

如果您使用Julia 0.7(基本上是==1.0,带有弃用),您将看到预期行为更改所需的弃用消息:

┌ 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
因此,为了得到你想要的,你可以写:

function main()
    global j = 666
    for j = 1:2
        println("j is $j")
    end
    println("global j is $j")
end

main() 
理论上,您在全局级别上的第一个示例应使用中所述的外部i.进行处理,但目前在REPL中未处理此问题。请参阅此问题:

如果使用Julia 0.7(基本上是==1.0,带有弃用),您将看到预期行为更改所需的弃用消息:

┌ 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
因此,为了得到你想要的,你可以写:

function main()
    global j = 666
    for j = 1:2
        println("j is $j")
    end
    println("global j is $j")
end

main() 

理论上,您在全局级别上的第一个示例应使用中所述的外部i.进行处理,但目前在REPL中未处理此问题。请参阅本期:

C程序员会没事的。for循环现在有效地执行了for(inti=1;iC程序员)的
for(inti=1;iC程序员就可以了。for循环现在有效地执行了for(inti=1;i)的
for(inti=1;iC程序员就可以了