Non-local variable
In
In Lua they are called the upvalues of the function.[1]
Examples
Nested functions
In the Python 3 example that follows there is a nested function inner
defined in the scope of another function outer
. The variable x
is local to outer
, but non-local to inner
(nor is it global):
def outer():
x = 1
def inner():
nonlocal x
x += 1
print(x)
return inner
In Javascript, the locality of a variable is determined by the closest var
statement for this variable. In the following example, x
is local to outer
as it contains a var x
statement, while inner
doesn't. Therefore, x is non-local to inner
:
function outer() {
var x = 1;
function inner() {
x += 1;
console.log(x);
}
return inner;
}
Anonymous functions
In the Haskell example that follows the variable c
is non-local in the anonymous function \x -> x + c
:
outer = let c = 1 in map (\x -> x + c) [1, 2, 3, 4, 5]
Implementation issues
Non-local variables are the primary reason it is difficult to support nested, anonymous, higher-order and thereby first-class functions in a programming language.
If the nested function or functions are (mutually)
If the nested function is passed as an argument to a higher-order function a
Notes
References
- Aho, Lam, Sethi, and Ullman. "7.3 Access to Nonlocal Data on the Stack". Compilers: Principles, Techniques, & Tools. Second edition.