julia> computername = "s"
"s"
julia> try
computername = ENV["COMPUTERNAME"]
catch
computername = ENV["HOSTNAME"]
end
"DESKTOP-FDIN0I7"
julia> computername
"DESKTOP-FDIN0I7"
这是终端运行的。(不过“软作用域”不是不会遮蔽全局变量吗?为啥它和for
都能改全局变量?)
但是把这段代码搬到VSCode中运行有点不同:
┌ Warning: Assignment to `computername` in soft scope is ambiguous because a global variable by the same name exists: `computername` will be treated as a new local. Disambiguate by using `local computername` to suppress this warning or `global computername` to assign to the existing global variable.
└ @ d:\Documents\Julia\20221027VSCode远程调用Julia测试\test.jl:4
┌ Warning: Assignment to `computername` in soft scope is ambiguous because a global variable by the same name exists: `computername` will be treated as a new local. Disambiguate by using `local computername` to suppress this warning or `global computername` to assign to the existing global variable.
└ @ d:\Documents\Julia\20221027VSCode远程调用Julia测试\test.jl:6
s
我只有在前面加上global
才能达到我想要的结果。怎么这里又突然遮蔽不了全局变量呢?
相反,for
无论在终端还是VSCode里面,都是可以改外面同名变量的(话说这还是“软作用域”吗?)。