I have the following scenario for initialization of a global variable which fails for some mysterious reason.
// t.jl
n = 1
function f()
if n == 1
n = 2
end
println(n)
end
...
julia> include("t.jl")
f (generic function with 1 method)
julia> f()
ERROR: UndefVarError: n not defined
Stacktrace:
[1] f() at t.jl:3
While it is perfectly clear to, e.g., just print a global variable:
// t.jl
n = 1
function f()
println(n)
end
...
julia> include("t.jl")
f (generic function with 1 method)
julia> f()
1
Can anyone explain the reason which causes global variable disappear? What are the limitations on using global variables?