Closure (computer programming)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 19 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope. def f(x: int) -> Callable[[int], int]: def g(y: int) -> int: return x + y return g # Return a closure. def h(x: int) -> Callable[[int], int]: return lambda y: x + y # Return a closure. Assigning specific closures to variables. a: Callable[[int], int] = f(1) b: Callable[[int], int] = h(1) Using the closures stored in variables. assert a(5)

6 assert b(5)

6 Using closures without binding them to variables first. assert f(1)(5)

6 # f(1) is the closure. assert h(1)(5)

6 # h(1) is the closure. the values of a and b are closures, in both cases produced by returning a nested function with a free variable from the enclosing function, so that the free variable binds to the value of parameter x of the enclosing function. The closures in a and b are functionally identical. The only difference in implementation is that in the first case we used a nested function with a name, g , while in the second case we used an anonymous nested function (using the Python keyword lambda for creating an anonymous function). The original name, if any, used in defining them is irrelevant. A closure is a value like any other value. It does not need to be assigned to a variable and can instead be used directly, as shown in the last two lines of the example. This usage may be deemed an "anonymous closure". The nested function definitions are not themselves closures: they have a free variable which is not yet bound. Only once the enclosing function is evaluated with a value for the parameter is the free variable of the nested function bound, creating a closure, which is then returned from the enclosing function. Lastly, a closure is only distinct from a function with free variables when outside of the scope of the non-local variables, otherwise the defining environment and the execution environment coincide and there is nothing to distinguish these (static and dynamic binding cannot be distinguished because the names resolve to the same values). For example, in the program below, functions with a free variable x (bound to the non-local variable x with global scope) are executed in the same environment where x is defined, so it is immaterial whether these are actually closures: x: int = 1 nums: list[int] = [1, 2, 3] def f(y: int) -> int: return x + y map(f, nums) map(lambda y: x + y, nums) This is most often achieved by a function return, since the function must be defined within the scope of the non-local variables, in which case typically its own scope will be smaller. This can also be achieved by variable shadowing (which reduces the scope of the non-local variable), though this is less common in practice, as it is less useful and shadowing is discouraged. In this example f can be seen to be a closure because x in the body of f is bound to the x in the global namespace, not the x local to g : x: int = 0 def f(y: int) -> int: return x + y def g(z: int) -> int: x: int = 1 # local x shadows global x return f(z) print(g(1)) prints 1, not 2 Return a list of all books with at least THRESHOLD copies sold. (define (best-selling-books threshold) (filter (lambda (book) (>= (book-sales book) threshold)) book-list)) In this example, the lambda expression (lambda (book) (>= (book-sales book) threshold)) appears within the function best-selling-books . When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda expression and a reference to the threshold variable, which is a free variable inside the lambda expression. The closure is then passed to the filter function, which calls it repeatedly to determine which books are to be added to the result list and which are to be discarded. Because the closure has a reference to threshold , it can use that variable each time filter calls it. The function filter might be defined in a separate file. Here is the same example rewritten in TypeScript, another popular language with support for closures: // Return a list of all books with at least 'threshold' copies sold. function bestSellingBooks(threshold: number): Book[] { return bookList.filter(book => book.sales >= threshold); } The arrow operator => is used to define an arrow function expression, and an Array.prototype.filter method instead of a global filter function, but otherwise the structure and the effect of the code are the same. A function may create a closure and return it, as in this example: type UnaryFunction = (x: number) => number; // Return a function that approximates the derivative of f // using an interval of dx, which should be appropriately small. function derivative(f: UnaryFunction, dx: number): UnaryFunction { return x => (f(x + dx) - f(x)) / dx; } Because the closure in this case outlives the execution of the function that creates it, the variables f and dx live on after the function derivative returns, even though execution has left their scope and they are no longer visible. In languages without closures, the lifetime of an automatic local variable coincides with the execution of the stack frame where that variable is declared. In languages with closures, variables must continue to exist as long as any existing closures have references to them. This is most commonly implemented using some form of garbage collection. (define foo #f) (define bar #f) (let ((secret-message "none")) (set! foo (lambda (msg) (set! secret-message msg))) (set! bar (lambda () secret-message))) (display (bar)) ; prints "none" (newline) (foo "meet me by the docks at midnight") (display (bar)) ; prints "meet me by the docks at midnight" Closures can be used to implement object systems. Note: Some speakers call any data structure that binds a lexical environment a closure, but the term usually refers specifically to functions. let f: () => number; let g: () => number; function foo() { let x: number; f = () => ++x; g = () => --x; x = 1; console.log(`inside foo, call to f(): ${f()}`); } foo(); // 2 console.log(`call to g(): ${g()}`); // 1 (--x) console.log(`call to g(): ${g()}`); // 0 (--x) console.log(`call to f(): ${f()}`); // 1 (++x) console.log(`call to f(): ${f()}`); // 2 (++x) Function foo and the closures referred to by variables f and g all use the same relative memory location signified by local variable x . In some instances the above behaviour may be undesirable, and it is necessary to bind a different lexical closure. Again in ECMAScript, this would be done using the Function.bind() .

Example 1: Reference to an unbound variable

var module = { x: 42, get() { return this.x; } } var unboundGet = module.get; console.log(unboundGet()); // The function gets invoked at the global scope // emits undefined as 'x' is not specified in global scope. var boundGet = unboundGet.bind(module); // specify object module as the closure console.log(boundGet()); // emits 42

Example 2: Accidental reference to a bound variable

For this example the expected behaviour would be that each link should emit its id when clicked; but because the variable 'e' is bound to the scope above, and lazy evaluated on click, what actually happens is that each on click event emits the id of the last element in 'elements' bound at the end of the for loop. var elements = document.getElementsByTagName('a'); // Incorrect: e is bound to the function containing the 'for' loop, not the closure of "handle" for (var e of elements) { e.onclick = function handle() { console.log(e.id); } } Again here variable e would need to be bound by the scope of the block using handle.bind(this) or the let keyword. On the other hand, many functional languages, such as ML, bind variables directly to values. In this case, since there is no way to change the value of the variable once it is bound, there is no need to share the state between closures—they just use the same values. This is often called capturing the variable "by value". Java's local and anonymous classes also fall into this category—they require captured local variables to be final , which also means there is no need to share state. Some languages enable choosing between capturing the value of a variable or its location. For example, in C++11, captured variables are either declared with [&] , which means captured by reference, or with [=] , which means captured by value. Yet another subset, lazy functional languages such as Haskell, bind variables to results of future computations rather than values. Consider this example in Haskell: -- Haskell foo :: Fractional a => a -> a -> (a -> a) foo x y = (\z -> z + r) where r = x / y f :: Fractional a => a -> a f = foo 1 0 main = print (f 123) The binding of r captured by the closure defined within function foo is to the computation (x / y) —which in this case results in division by zero. However, since it is the computation that is captured, and not the value, the error only manifests when the closure is invoked, and then attempts to use the captured binding. "Smalltalk" foo | xs | xs := #(1 2 3 4). xs do: [:x | ^x]. ^0 bar Transcript show: (self foo printString) "prints 1" // ECMAScript function foo(): number { let xs: number[] = [1, 2, 3, 4]; xs.forEach(function (x) { return x; }); return 0; } console.log(foo()); // prints 0 The above code snippets will behave differently because the Smalltalk ^ operator and the JavaScript return operator are not analogous. In the ECMAScript example, return x; will leave the inner closure to begin a new iteration of the forEach loop, whereas in the Smalltalk example, ^x will abort the loop and return from the method foo . Common Lisp provides a construct that can express either of the above actions: Lisp (return-from foo x) behaves as Smalltalk ^x , while Lisp (return-from nil x) behaves as JavaScript return x . Hence, Smalltalk makes it possible for a captured escape continuation to outlive the extent in which it can be successfully invoked. Consider: "Smalltalk" foo ^[ :x | ^x ] bar | f | f := self foo. f value: 123 "error!" When the closure returned by the method foo is invoked, it attempts to return a value from the invocation of foo that created the closure. Since that call has already returned and the Smalltalk method invocation model does not follow the spaghetti stack discipline to facilitate multiple returns, this operation results in an error. Some languages, such as Ruby, enable the programmer to choose the way return is captured. An example in Ruby: Ruby Closure using a Proc def foo f = Proc.new { return "return from foo from inside proc" } f.call # control leaves foo here return "return from foo" end Closure using a lambda def bar f = lambda { return "return from lambda" } f.call # control does not leave bar here return "return from bar" end puts foo # prints "return from foo from inside proc" puts bar # prints "return from bar" Both Proc.new and lambda in this example are ways to create a closure, but semantics of the closures thus created are different with respect to the return statement. In Scheme, definition and scope of the return control statement is explicit (and only arbitrarily named 'return' for the sake of the example). The following is a direct translation of the Ruby sample. Scheme (define call/cc call-with-current-continuation) (define (foo) (call/cc (lambda (return) (define (f) (return "return from foo from inside proc")) (f) ; control leaves foo here (return "return from foo")))) (define (bar) (call/cc (lambda (return) (define (f) (call/cc (lambda (return) (return "return from lambda")))) (f) ; control does not leave bar here (return "return from bar")))) (display (foo)) ; prints "return from foo from inside proc" (newline) (display (bar)) ; prints "return from bar"

Nested function and function pointer (C)

With a GNU Compiler Collection (GCC) extension, a nested function can be used and a function pointer can emulate closures, provided the function does not exit the containing scope. The next example is invalid because adder is a top-level definition (depending on compiler version, it could produce a correct result if compiled with no optimizing, i.e., at -O0 ): include typedef int (*FnIntToInt)(int); // type of function int->int FnIntToInt adder(int number) { int add(int value) { return value + number; } return &add; // & operator is optional here because the name of a function in C is a pointer pointing on itself } int main(void) { FnIntToInt add10 = adder(10); printf("%d\n", add10(1)); return 0; } But moving adder (and, optionally, the typedef ) in main makes it valid: include typedef int (*FnIntToInt)(int); // type of function int->int int main(void) { FnIntToInt adder(int number) { int add (int value) { return value + number; } return add; } FnIntToInt add10 = adder(10); printf("%d\n", add10(1)); return 0; } If executed this now prints 11 as expected. import java.net.URI; import javax.swing.JFrame; class CalculationWindow extends JFrame { private volatile int result; // ... public void calculateInSeparateThread(final URI uri) { // The expression "new Runnable() { ... }" is an anonymous class implementing the 'Runnable' interface. Thread t = new Thread( new Runnable() { @Override void run() { // It can read final local variables: calculate(uri); // It can access private fields of the enclosing class: result += 10; } } ); t.start(); } } The capturing of final variables enables capturing variables by value. Even if the variable to capture is non- final , it can always be copied to a temporary final variable just before the class. Capturing of variables by reference can be emulated by using a final reference to a mutable container, for example, a one-element array. The local class will not be able to change the value of the container reference, but it will be able to change the contents of the container. With the advent of Java 8's lambda expressions, the closure causes the above code to be executed as: import java.net.URI; import javax.swing.JFrame; class CalculationWindow extends JFrame { private volatile int result; // ... public void calculateInSeparateThread(final URI uri) { // The code () -> { /* code */ } is a closure. Thread t = new Thread(() -> { calculate(uri); result += 10; }); t.start(); } } Local classes are one of the types of inner class that are declared within the body of a method. Java also supports inner classes that are declared as non-static members of an enclosing class. They are normally referred to just as "inner classes". These are defined in the body of the enclosing class and have full access to instance variables of the enclosing class. Due to their binding to these instance variables, an inner class may only be instantiated with an explicit binding to an instance of the enclosing class using a special syntax. public class EnclosingClass { // Define the inner class: public class InnerClass { public int incrementAndReturnCounter() { return counter++; } } private int counter; { counter = 0; } public int getCounter() { return counter; } public static void main(String[] args) { EnclosingClass enclosingClassInstance = new EnclosingClass(); // Instantiate the inner class, with binding to the instance EnclosingClass.InnerClass innerClassInstance = enclosingClassInstance.new InnerClass(); for ( int i = enclosingClassInstance.getCounter(); (i = innerClassInstance.incrementAndReturnCounter()) Upon execution, this will print the integers from 0 to 9. Beware to not confuse this type of class with the nested class, which is declared in the same way with an accompanied usage of the "static" modifier; those have not the desired effect but are instead just classes with no special binding defined in an enclosing class. As of Java 8, Java supports functions as first class objects. Lambda expressions of this form are considered of type java.util.function.Function with T being the domain and U the image type. The expression can be called with its Function.apply(T t) method, but not with a standard method call. import java.util.function.Function; Function length = s -> s.length(); System.out.println(length.apply("Hello, world!")); // prints '13' typedef int (^IntBlock)(); IntBlock downCounter(int start) { __block int i = start; return [[ ^int() { return i--; } copy] autorelease]; } IntBlock f = downCounter(5); NSLog(@"%d", f()); NSLog(@"%d", f()); NSLog(@"%d", f()); using System.Collections.Generic; int[] data = new[]{1, 2, 3, 4}; int multiplier = 2; List result = data.Select(x => x * multiplier); Visual Basic .NET, which has many language features similar to those of C#, also supports lambda expressions with closures: Dim data = {1, 2, 3, 4} Dim multiplier = 2 Dim result = data.Select(Function(x) x * multiplier) In D, closures are implemented by delegates, a function pointer paired with a context pointer (e.g. a class instance, or a stack frame on the heap in the case of closures). auto test1() { int a = 7; return delegate() { return a + 3; }; // anonymous delegate construction } auto test2() { int a = 20; int foo() { return a + 5; } // inner function return &foo; // other way to construct delegate } void bar() { auto dg = test1(); dg(); // 10 (OK: test1.a is in a closure and still exists) dg = test2(); dg(); // 25 (OK: test2.a is in a closure and still exists) } D version 1, has limited closure support. For example, the above code will not work correctly, because the variable a is on the stack, and after returning from test(), it is no longer valid to use it (most probably calling foo via dg(), will return a 'random' integer). This can be solved by explicitly allocating the variable 'a' on heap, or using structs or class to store all needed closed variables and construct a delegate from a method implementing the same code. Closures can be passed to other functions, as long as they are only used while the referenced values are still valid (for example calling another function with a closure as a callback parameter), and are useful for writing generic data processing code, so this limitation, in practice, is often not an issue. This limitation was fixed in D version 2 - the variable 'a' will be automatically allocated on the heap because it is used in the inner function, and a delegate of that function can escape the current scope (via assignment to dg or return). Any other local variables (or arguments) that are not referenced by delegates or that are only referenced by delegates that do not escape the current scope, remain on the stack, which is simpler and faster than heap allocation. The same is true for inner's class methods that reference a function's variables. import std; using std::string; using std::vector; void foo(string& name) { int n = 100; vector v; // ... // i is of type vector ::iterator auto i = std::ranges::find_if(v, [&](const string& s) -> bool { return s != name && s.size() > n; }); // 'i' is now either 'v.end()' or points to the first string in 'v' // which is not equal to 'name' and whose length is greater than 'n' } C++ also has the object std::function for holding an anonymous function. However, because anonymous functions have compiler-generated names and are distinct, storing one as a std::function uses type erasure which causes minor overhead. ok_button.click_event.subscribe ( agent (x, y: INTEGER) do map.country_at_coordinates (x, y).display end ) the argument to subscribe is an agent, representing a procedure with two arguments; the procedure finds the country at the corresponding coordinates and displays it. The whole agent is "subscribed" to the event type click_event for a certain button, so that whenever an instance of the event type occurs on that button – because a user has clicked the button – the procedure will be executed with the mouse coordinates being passed as arguments for x and y . The main limitation of Eiffel agents, which distinguishes them from closures in other languages, is that they cannot reference local variables from the enclosing scope. This design decision helps in avoiding ambiguity when talking about a local variable value in a closure - should it be the latest value of the variable or the value captured when the agent is created? Only Current (a reference to current object, analogous to this in Java), its features, and arguments of the agent can be accessed from within the agent body. The values of the outer local variables can be passed by providing additional closed operands to the agent. Standard C allows writing a for a pointer to a function type using the following syntax: typedef void (*TMyFunctionPointer)(void); In a similar way, a typedef can be declared for a pointer to a method using this syntax: typedef void (__closure *TMyMethodPointer)();

Text derived from https://en.wikipedia.org/w/index.php?oldid=1373903271 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

19 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.