This is the hard local scope of functions, which is currently not documented. The rule is that inside a function any variable assignment which would write to a *global* variable, makes that variable local. See below for inline comments.
Funnily enough, I just rewrote the scope section of the manual which now includes that rule. You seem uniquely qualified to read it and give feedback: https://github.com/JuliaLang/julia/pull/12146 a semi-rendered version is here: https://github.com/mauro3/julia/blob/41911d96d86397168ade1f39914a8d7ed1653558/doc/manual/variables-and-scoping.rst#id7 Thanks! On Wed, 2015-07-15 at 20:40, Ethan Anderes <[email protected]> wrote: > Hi Everyone. I hope this isn’t a stupid question but I can’t seem to > understand the following > scoping behavior. The basic story is that mesh in the following two > examples behaves differently when defined in global scope vrs within a > function. I’m worried that I’m completely ignorant of how functions work > within other functions. Some help would be much appreciated. Thanks! > > In this block of code mesh is defined as a global variable. > > julia> mesh(y) = (x = repmat(y.', 2, 1); return x) > mesh (generic function with 1 method) Here x is implicitly local as assigning to it would result in a modified global. > julia> x = mesh(1:2); > > julia> y = mesh(2:3); Thus above call will *not* modify x. > julia> x > 2x2 Array{Int64,2}: > 1 2 > 1 2 > > When I quit, restart Julia and define mesh within foo I get a different > answer. > > julia> function foo() > mesh(y) = (x = repmat(y.', 2, 1); return x) Here x is inherited from the scope of foo as x is a local variable. > x = mesh(1:2) > y = mesh(2:3) # <- this line is necessary to see the effect Thus above call will modify x. > return x > end > foo (generic function with 1 method) > > julia> x = foo() # <- different than the x in the global case. > 2x2 Array{Int64,2}: > 2 3 > 2 3 > > Here is my version info > > julia> versioninfo() > Julia Version 0.4.0-dev+5994 > Commit 8efc44d (2015-07-15 15:42 UTC) > Platform Info: > System: Darwin (x86_64-apple-darwin14.4.0) > CPU: Intel(R) Core(TM) M-5Y51 CPU @ 1.10GHz > WORD_SIZE: 64 > BLAS: libopenblas (USE64BITINT NO_AFFINITY HASWELL) > LAPACK: libopenblas > LIBM: libopenlibm > LLVM: libLLVM-3.3 > >
