Thanks Mauro. I took a look at your new manual entry for scoping rules. Very nice. It clears up a lot of questions. I still have two more.
Question 1) In your post you say that…. The rule is that inside a function any variable assignment which would write to a *global* variable, makes that variable local. I guess I still don’t understand how the nested function scoping behavior is described by the above sentence. Maybe I don’t understand what you mean by *global* and how I could determine if an assignment “would write to a *global* variable”. My best attempt at describing this behavior in my own way would be as follows: the assignment x = repmat(y.', 2, 1) within mesh within foo treats x as “local-to-foo” and “global-to-mesh”. Is this consistent with what your saying? Question 2) What advantage is there for changing the scoping rules for nested functions? I did notice the following example in your manual but it is not clear to me how it (or if it was intended to) explain the usefulness of different scoping rules for nested functions. even(n) = n == 0 ? true : odd(n-1) odd(n) = n == 0 ? false : even(n-1) julia> even(3) false julia> odd(3) true On Wednesday, July 15, 2015 at 1:29:40 PM UTC-7, Mauro wrote: 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] > <javascript:>> 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 > > > > > >
