When there is an ambiguity in the scope of a variable (same name inside and outside a for loop), Julia defaults to totally different behaviours when the code is run interactively in the REPL or as a script.
x = range(1, stop=3);
for i in 1:3
x = x .+ 1
end
println(x)
Output when run interactively in the Julia REPL:
4:6
Output when run as a script (julia xxx.jl
):
┌ Warning: Assignment to `x` in soft scope is ambiguous because a global variable by the same name exists: `x` will be treated as a new local. Disambiguate by using `local x` to suppress this warning or `global x` to assign to the existing global variable.
â”” @ xxx.jl:4
ERROR: LoadError: UndefVarError: x not defined
Stacktrace:
[1] top-level scope at xxx.jl:4
[2] include(::Function, ::Module, ::String) at ./Base.jl:380
[3] include(::Module, ::String) at ./Base.jl:368
[4] exec_options(::Base.JLOptions) at ./client.jl:296
[5] _start() at ./client.jl:506
in expression starting at xxx.jl:3
Of course, getting rid of the ambiguity and writing a better code such as:
x = range(1, stop=3);
for i in 1:3
global x = x .+ 1
end
println(x)
solves the problem as both the interactive and non interactive runs give 4:6
.
However, I would expect to get the same behaviour, no matter how the code is run.
It is surprising to me that I do not get warned about the ambiguity in the scope of x
when the code is run in the REPL.
But even much more surprising to me is that, to solve this ambiguity, Julia defaults to different scopes depending on how the code is run:
I am quite new to Julia and there may be well established and good reasons for this. If so, my apologies and I'll be curious to learn about those. But I thought I would mention it in case this might actually be a bug.
Thank you.
Julia Version 1.5.2
Commit 539f3ce943 (2020-09-23 23:17 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-9.0.1 (ORCJIT, broadwell)
I belive this behavior is expected: https://docs.julialang.org/en/v1/manual/variables-and-scoping/#On-Soft-Scope
Ah! I thought it might be my ignorance about Julia :roll_eyes:
Thank you for the quick reply. I guess I should close this issue and go do my homework (I need to read why the behaviour would be different by design!).
Sorry for the noise... :slightly_frowning_face:
Just read the justification for the difference in behaviour. Thanks again @fredrikekre for pointing me to the right documentation (which I should of course have read before opening this issue :slightly_smiling_face: ).
No worries. And please keep reporting any problems you find; the docs definitely aren't perfect, and your report (even if you now feel it to be misguided) was thorough, constructive, and (had there been a problem) eminently actionable. Definitely the kind of report you want to receive more of.
Most helpful comment
No worries. And please keep reporting any problems you find; the docs definitely aren't perfect, and your report (even if you now feel it to be misguided) was thorough, constructive, and (had there been a problem) eminently actionable. Definitely the kind of report you want to receive more of.