4 ms·
As noted in the rewritten scope docs at https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope-1 https://docs.julialang.org/en/v1/manual/var
by StefanKarpinski 6y ago
As noted in the rewritten scope docs at https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope-1 https://docs.julialang.org/en/v1/manual/variables-and-scopin...:
> An important property of this design is that any code that executes in a file without a warning will behave the same way in a fresh REPL. And on the flip side, if you take a REPL session and save it to file, if it behaves differently than it did in the REPL, then you will get a warning.
So there's no real danger of deviating behavior unless you're in the habit of ignoring warnings.
As to why bringing back 0.x scope rules is not a good idea, this post explains why it was changed in the first place: https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/22 https://discourse.julialang.org/t/explain-scoping-confusion-.... In short, for non-toy programs, it's a significant source of hard-to-find bugs.
Also: changing this behavior in non-interactive contexts would violate the semantic versioning compatibility commitment of 1.x releases. Of course, 1.5 does sometimes introduce a warning in existing code, but (a) it will keep working the same way it did only with a warning and (b) this only happens if you had a global implicitly shadowed by a local, in which case there's a large chance your code was actually broken.
Give it a try. A lot of time and thought was put into this change and we believe the new design is pretty much locally optimal. It preserves the safety of the 1.0 behavior for non-interactive programming while recovering the convenience of the 0.x behavior in the REPL.
- UncleOxidant 6y ago> 0.x scope rules... In short, for non-toy programs, it's a significant source of hard-to-find bugs. It seems like a lot of languages have lexical scoping which IIRC was what 0.x had. (Scheme, Lisp, OCaml are a few examples) Are there aspects of Julia that make lexical scoping particularly problematic? Julia 1.x scoping rules look similar to Tcl's.
- StefanKarpinski 6y agoJulia has utterly standard lexical scoping—and always has. This issue has nothing to do with lexical vs dynamic scoping, it has to do with whether an assignment in a loop clobbers a global (convenient but dangerous) or creates a loop-local variable (safe but sometimes inconvenient). Have you read the linked manual section on scope? It is very clear and explains the design considerations quite thoroughly.