Non-local variable
   HOME

TheInfoList



OR:

In
programming language theory Programming language theory (PLT) is a branch of computer science that deals with the design, implementation, analysis, characterization, and classification of formal languages known as programming languages. Programming language theory is clos ...
, a non-local variable is a variable that is not defined in the local
scope Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * Cinem ...
. While the term can refer to
global variable In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global s ...
s, it is primarily used in the context of
nested ''Nested'' is the seventh studio album by Bronx-born singer, songwriter and pianist Laura Nyro, released in 1978 on Columbia Records. Following on from her extensive tour to promote 1976's ''Smile'', which resulted in the 1977 live album '' Seas ...
and
anonymous function In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to ...
s where some variables can be in neither the
local Local may refer to: Geography and transportation * Local (train), a train serving local traffic demand * Local, Missouri, a community in the United States * Local government, a form of public administration, usually the lowest tier of administrat ...
nor the
global scope In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts ...
. In
Lua Lua or LUA may refer to: Science and technology * Lua (programming language) * Latvia University of Agriculture * Last universal ancestor, in evolution Ethnicity and language * Lua people, of Laos * Lawa people, of Thailand sometimes referred t ...
they are called the ''upvalues'' of the function.
Programming in Lua (first edition)
''


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()


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) , 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 function In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from ...
s in a programming language. If the nested function or functions are (mutually)
recursive Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
, it becomes hard for the
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
to know exactly where on the
call stack In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or ma ...
the non-local variable was allocated, as the
frame pointer In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mach ...
only points to the local variable of the nested function itself and there can be an arbitrary number of
activation record In computer science, a call stack is a Stack (abstract data type), stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control st ...
s on the stack in between. This is generally solved using
access link In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or m ...
s or
display register In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or m ...
s. If the nested function is passed as an argument to a higher-order function a closure needs to be built in order to locate the non-local variables. If the nested function is returned as a result from its outer function (or stored in a variable) the non-local variables will no longer be available on the stack. They need to be heap allocated instead, and their lifetime extends beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.


Notes

{{reflist


References

* Aho, Lam, Sethi, and Ullman. "7.3 Access to Nonlocal Data on the Stack". '' Compilers: Principles, Techniques, & Tools''. Second edition. Programming language theory Variable (computer science)