Support writing to variables in outer, non-global scope
In Julia, assignments in functions of the form function (args); body; end create a new local variable by default rather than writing to variables in the outer scope. There is no way to declare in a deeply nested function that a variable belongs to the scope immediately surrounding it, not the global scope. This proposes to add a feature so as to remove the restriction.
This is called nonlocal in Python, so that would be a sane choice for a keyword.
Not really correct:
julia> function foo()
x = 1
g = function (y)
x = y
end
g(42)
x
end
foo (generic function with 1 method)
julia> foo()
42
Do you have a particular example of code that didn't work as it should?
Of course, if x is shadowed in the inner function by writing local x, then you lose access to the outer x. That seems acceptable to me though.
We can reopen if there turns out to be an issue here.
Part of this confusion may be the difference between working at global scope e.g. from the REPL vs. inside a function. For example:
From within a function the inner (anonymous) function accesses the outer scope:
julia> function testfunc()
x = 1
open("/dev/null") do f
x = 2
end
x
end
testfunc (generic function with 1 method)
julia> testfunc()
2
vs at global scope:
julia> x = 1
1
julia> open("/dev/null") do f
x = 2
end
2
julia> x
1
This just bit me when trying to read audio from a file in an IJulia notebook by doing
clarinet1::Array{Float32}
AudioIO.open("data/mirex05/sounds/clarinet1.wav") do f
clarinet1 = read(f, Float32)
end
Which seems like it would be a pretty common pattern.
In general this confusion between the way things behave and perform at the REPL/IJulia and the way things happen inside a module or function seems to bite me over and over again, and it's just now after getting some more Julia experience under my belt that I'm getting to be on the watch for it. I know there are a lot of reasonable reasons that it behaves the way it does, but I wanted to register my experience that it does cause quite a bit of confusion, and if there were a way to unify the behavior and performance it would be a good thing.
The answer is to avoid globals. Only assign globals from the top level; for example
clarinet1 = AudioIO.open("data/mirex05/sounds/clarinet1.wav") do f
read(f, Float32)
end
I'd like to argue that having a nonlocal keyword would make Julia better in the Python-zen sense: "Explicit is better than implicit." (see also discussion in #10559)
This would turn Jeff's example above into:
function foo()
x = 1
g = (y) -> nonlocal x = y
g(42)
x
end
One contrived example where nonlocal would avoid introducing a bug is:
f(x) = (y=1; x+y)
function usef(y)
f(y)
y
end
usef(5) # -> 5
# I refactor and copy f to inner scope:
function usef(y)
f(x) = (y=1; x+y)
f(y)
y
end
usef(5) # -> 1 !!!
Just like requiring an explicit global to modify global variables, requiring an explicit nonlocal makes sense (to me) to modify variables in an inner scope. If that convinced anyone with commit access, then please re-open this issue. (Of course, this would be breaking.)