Lesson plan

Non-local variable

In programming language theory, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables can be in neither the local nor the global scope. In Lua they are called the upvalues of the function. == 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): from typing import Callable def outer() -> Callable[[], None]: x: int = 1 def inner() -> None: 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]

Back to article

Article summary

In programming language theory, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables can be in neither the local nor the global scope. In Lua they are called the upvalues of the function. == 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): from typing import Callable def outer() -> Callable[[], None]: x: int = 1 def inner() -> None: 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]

Key claims

No claims recorded.

Discussion questions

  1. Summarise the article in your own words. Which single claim would you verify first, and why?
  2. Which claim in this article is most likely to be revised as the sources update? How would you check its current status?
  3. If two sources disagreed about one of these facts, what would you need in order to trust one over the other?

Bibliography

No sources recorded.

Key claims and bibliography are generated from the same tracked claims and sources as the article page.