currently if in a file i have
local s = 0
for i = 1:10
t = s + i
s = t
println("$s")
end
println("$s")
and I execute this file, I will get an error ERROR: LoadError: UndefVarError: s not definedcurrently if in a file i have
local s = 0
for i = 1:10
t = s + i
s = t
println("$s")
end
println("$s")
and I execute this file, I will get an error ERROR: LoadError: UndefVarError: s not definedYou can write this as either
s = 0
for i ∈ 1:10
t = s + i
global s = t
println("$s")
end
println("$s")
or let s = 0
for i ∈ 1:10
t = s + i
s = t
println("$s")
end
println("$s")
end function test()
s = 0
for i = 1:10
t = s + i
s = t
println("$s")
end
println("$s")
end
test()
For anyone curious, the rationale for this scoping is explained in the docs. Essentially it's to prevent the so-called spooky action at distance.Sure, there's things where I find it too fussy and I wish it'd go "yeah yeah I get what you mean" and there's also things where I find myself thinking "man, I really wish you had complained to me about that, it was clearly a bug / bad style!"
But all-in-all, I find it's a language that mostly gets out of my way and lets me code, which I find very useful and nice for scripting.
I think if one finds things like scoping rules annoying for scripting, that might just come down to just knowing and being more familiar with another language that has different rules.
Why not make this the default everywhere? Well, there are a lot of scientific use cases where it's convenient to have Python-style dynamic typing and interactivity. A cool thing about Julia is that it allows that, while _also_ allowing to achieve high-performance, all within a single language.
For the record, I do also love the static type system and overall design of Rust. But for my day job (research in numerical methods and computational physics), I find Julia to be the most efficient way to get the job done -- rapid algorithm prototyping, data analysis, plot generation, etc.
s is defined in the statement and then is undefined out of that statement because there is no other scope for it - it's at top level. just write s =0 and all is well as s exist in the global scope where your code lives as well...
[1]: https://docs.julialang.org/en/v1/manual/variables-and-scopin...
Although it's a bit of an challenge to get the interpreter to understand which thing that's out of scope should be in scope for the code to work (because it's all out of scope..)