Closure (computer programming): Difference between revisions
No edit summary |
m Open access bot: hdl updated in citation with #oabot. |
||
(452 intermediate revisions by more than 100 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description|Technique for creating lexically scoped first class functions}} |
|||
In [[computer science]], a '''closure''' (also '''lexical closure''', '''function closure''' or '''function value''') is a [[function (computer science)|function]] together with a referencing environment for the nonlocal names ([[Free variables and bound variables|free variable]]s) of that function.<ref>Sussman and Steele. "Scheme: An interpreter for extended lambda calculus". "... a data structure containing a lambda expression, and an environment to be used when that lambda expression is applied to arguments." ([http://en.wikisource.org/wiki/Page:Scheme_-_An_interpreter_for_extended_lambda_calculus.djvu/22 Wikisource])</ref> Such a function is said to be "closed over" its free variables. The referencing environment [[name binding|binds]] the nonlocal names to the corresponding variables in [[Scope (computer science)|scope]] at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure itself. |
|||
{{Other uses|Closure (mathematics)|Closure (disambiguation)}} |
|||
{{Distinguish|text=the programming language [[Clojure]]}} |
|||
{{Use dmy dates|date=August 2020}} |
|||
In [[programming language]]s, a '''closure''', also '''lexical closure''' or '''function closure''', is a technique for implementing [[lexically scoped]] [[name binding]] in a language with [[first-class function]]s. [[Operational semantics|Operationally]], a closure is a [[Record (computer science)|record]] storing a [[Function (computer science)|function]]{{efn|The function may be stored as a [[Reference (computer science)|reference]] to a function, such as a [[function pointer]].}} together with an environment.<ref>Sussman and Steele. "Scheme: An interpreter for extended lambda calculus". "... a data structure containing a lambda expression, and an environment to be used when that lambda expression is applied to arguments." ([[s:Page:Scheme - An interpreter for extended lambda calculus.djvu/22|Wikisource]])</ref> 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 (computer science)|value]] or [[Reference (computer science)|reference]] to which the name was bound when the closure was created.{{efn|These names usually refer to values, mutable variables, or functions, but can also be other entities such as constants, types, classes, or labels.}} 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. |
|||
== History and etymology == |
|||
The concept of closures was developed in the 1960s and was first fully implemented as a language feature in 1975 in the programming language [[Scheme (programming language)|Scheme]] to implement [[lexically-scoped]] [[first-class function]]s. Since then, many languages have been designed to support closures. The explicit use of closures is associated with [[functional programming]] and with languages such as [[ML (programming language)|ML]] and [[Lisp (programming language)|Lisp]]. Traditional imperative languages (such as [[Algol (programming language)|Algol]], [[C (programming language)|C]] and [[Pascal (programming language)|Pascal]]) had no support for closures as these languages neither support nonlocal names (which can be introduced only in nested or anonymous functions) nor [[higher-order function]]s. Modern garbage-collected imperative languages (such as [[Smalltalk]], the first object-oriented language featuring closures,<ref>[http://www.jugpadova.it/files/Closures_in_Java.pdf Closures in Java]{{Better source|date=September 2011}}</ref> [[C Sharp (programming language)|C#]], but notably not [[Java (programming language)|Java]]<ref>[[OpenJDK]]: [http://openjdk.java.net/projects/closures/ Closures for the Java Programming Language], [http://openjdk.java.net/projects/lambda/ Project Lambda]; [http://javac.info/ Closures (Lambda Expressions) for the Java Programming Language]; [[James Gosling]]. [http://blogs.oracle.com/jag/entry/closures "Closures"]; [[Guy Steele]]. [http://article.gmane.org/gmane.comp.lang.lightweight/2274 Re: bindings and assignments].</ref>) and many interpreted and scripting languages (Such as [[Lua|Lua]]), do support higher-order functions and closures. |
|||
The concept of closures was developed in the 1960s for the mechanical evaluation of expressions in the [[λ-calculus]] and was first fully implemented in 1970 as a language feature in the [[PAL (programming language)|PAL programming language]] to support lexically scoped [[first-class function]]s.<ref name=dat2012>{{cite conference |author-link=David A. Turner |first=David A. |last=Turner |year=2012 |url=http://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf |title=Some History of Functional Programming Languages |book-title=International Symposium on Trends in Functional Programming |pages=1–20 See 12 §2, note 8 for the claim about M-expressions |publisher=Springer |doi=10.1007/978-3-642-40447-4_1 |isbn=978-3-642-40447-4 |series=Lecture Notes in Computer Science |volume=7829}}</ref> |
|||
[[Peter Landin]] defined the term ''closure'' in 1964 as having an ''environment part'' and a ''control part'' as used by his [[SECD machine]] for evaluating expressions.<ref name=landin> |
|||
Closures are used to implement [[continuation-passing style]], and in this manner, [[information hiding|hide state]]. Constructs such as [[object (computer science)|object]]s and [[control structure]]s can thus be implemented with closures. In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At [[Run time (program lifecycle phase)|run-time]], when the outer function executes, a closure is formed, consisting of the inner function’s code and references to any variables of the outer function required by the closure; such variables are called the [[upvalue]]s of the closure. |
|||
{{cite journal |last=Landin |first=P.J. |author-link=Peter Landin |title=The mechanical evaluation of expressions |journal=The Computer Journal |volume=6 |issue=4 |date=January 1964 |pages=308–320 |doi=10.1093/comjnl/6.4.308 |url=https://academic.oup.com/comjnl/article-pdf/6/4/308/1067901/6-4-308.pdf }}</ref> [[Joel Moses]] credits Landin with introducing the term ''closure'' to refer to a [[Anonymous function|lambda expression]] with open bindings (free variables) that have been closed by (or bound in) the lexical environment, resulting in a ''closed expression'', or closure.<ref> |
|||
{{cite journal |last=Moses |first=Joel |author-link=Joel Moses |date=June 1970 |title=The Function of FUNCTION in LISP, or Why the FUNARG Problem Should Be Called the Environment Problem |journal=ACM SIGSAM Bulletin |issue=15 |pages=13–27 |doi=10.1145/1093410.1093411 |id=[[AI Memo]] 199 |quote=A useful metaphor for the difference between FUNCTION and QUOTE in LISP is to think of QUOTE as a porous or an open covering of the function since free variables escape to the current environment. FUNCTION acts as a closed or nonporous covering (hence the term "closure" used by Landin). Thus we talk of "open" Lambda expressions (functions in LISP are usually Lambda expressions) and "closed" Lambda expressions. [...] My interest in the environment problem began while Landin, who had a deep understanding of the problem, visited MIT during 1966–67. I then realized the correspondence between the FUNARG lists which are the results of the evaluation of "closed" Lambda expressions in [[LISP 1.5|LISP]] and [[ISWIM]]'s Lambda Closures.|hdl=1721.1/5854|s2cid=17514262 |hdl-access=free }}</ref><ref>{{cite book |last=Wikström |first=Åke |year=1987 |title=Functional Programming using Standard ML |publisher=Prentice Hall |isbn=0-13-331968-7 |quote=The reason it is called a "closure" is that an expression containing free variables is called an "open" expression, and by associating to it the bindings of its free variables, you close it.}}</ref> This use was subsequently adopted by [[Gerald Jay Sussman|Sussman]] and [[Guy L. Steele Jr.|Steele]] when they defined [[Scheme (programming language)|Scheme]] in 1975,<ref>{{cite report |last1=Sussman |first1=Gerald Jay |author1-link=Gerald Jay Sussman |last2=Steele |first2=Guy L. Jr. |author2-link=Guy L. Steele Jr. |date=December 1975 |title=Scheme: An Interpreter for the Extended Lambda Calculus |id=[[AI Memo]] 349}}</ref> a lexically scoped variant of [[Lisp (programming language)|Lisp]], and became widespread. |
|||
Sussman and [[Harold Abelson|Abelson]] also use the term ''closure'' in the 1980s with a second, unrelated meaning: the property of an operator that adds data to a [[data structure]] to also be able to add nested data structures. This use of the term comes from [[Closure (mathematics)|mathematics use]], rather than the prior use in computer science. The authors consider this overlap in terminology to be "unfortunate."<ref>{{cite book |last1=Abelson |first1=Harold |author1-link=Harold Abelson |last2=Sussman |first2=Gerald Jay |author2-link=Gerald Jay Sussman |last3=Sussman |first3=Julie |author3-link=Julie Sussman |date=1996 |title=Structure and Interpretation of Computer Programs |url=https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book.html |publisher=MIT Press |pages=98–99 |isbn=0-262-51087-1}}</ref> |
|||
The term ''closure'' is often mistakenly used to mean [[anonymous function]]. This is probably because most languages implementing anonymous functions allow them to form closures and programmers are usually introduced to both concepts at the same time. These are, however, distinct concepts. A closure retains a reference to the environment at the time it was created (for example, to the current value of a local variable in the enclosing scope) while a generic anonymous function need not do this. |
|||
== Anonymous functions == |
|||
Closures are closely related to [[function object]]s; the transformation from the former to the latter is known as [[defunctionalization]] or [[lambda lifting]]. |
|||
{{further|Anonymous function}} |
|||
The term ''closure'' is often used as a synonym for [[anonymous function]], though strictly, an anonymous function is a function [[Literal (computer programming)|literal]] without a name, while a closure is an instance of a function, a [[Value (computer science)|value]], whose non-local variables have been bound either to values or to [[Variable (computer science)|storage locations]] (depending on the language; see the [[#Lexical environment|lexical environment]] section below). |
|||
For example, in the following [[Python (programming language)|Python]] code: |
|||
== Etymology == |
|||
<syntaxhighlight lang="python"> |
|||
def f(x): |
|||
def g(y): |
|||
return x + y |
|||
return g # Return a closure. |
|||
def h(x): |
|||
[[Peter J. Landin]] defined the term ''closure'' in 1964 as having an ''environment part'' and a ''control part'' as used by his [[SECD machine]] for evaluating expressions.<ref>{{cite |
|||
return lambda y: x + y # Return a closure. |
|||
| author = [[Peter J. Landin|P. J. Landin]] |
|||
| year = 1964 |
|||
# Assigning specific closures to variables. |
|||
| title = The mechanical evaluation of expressions |
|||
a = f(1) |
|||
}}</ref> |
|||
b = h(1) |
|||
[[Joel Moses]] credits Landin with introducing the term ''closure'' to refer to a [[lambda expression]] whose open bindings (free variables) have been closed by (or bound in) the lexical environment, resulting in a ''closed expression'', or closure.<ref>{{cite |
|||
| author = [[Joel Moses]] |
|||
# Using the closures stored in variables. |
|||
| month = June |
|||
assert a(5) == 6 |
|||
assert b(5) == 6 |
|||
| title = The Function of FUNCTION in LISP, or Why the FUNARG Problem Should Be Called the Environment Problem |
|||
| id = [[AI Memo]] 199 |
|||
# Using closures without binding them to variables first. |
|||
| url = http://dspace.mit.edu/handle/1721.1/5854 |
|||
assert f(1)(5) == 6 # f(1) is the closure. |
|||
| format = PDF |
|||
assert h(1)(5) == 6 # h(1) is the closure. |
|||
| accessdate = 2009-10-27 |
|||
</syntaxhighlight> |
|||
| quote = A useful metaphor for the difference between FUNCTION and QUOTE in LISP is to think of QUOTE as a porous or an open covering of the function since free variables escape to the current environment. FUNCTION acts as a closed or nonporous covering (hence the term "closure" used by Landin). Thus we talk of "open" Lambda expressions (functions in LISP are usually Lambda expressions) and "closed" Lambda expressions. [...] My interest in the environment problem began while Landin, who had a deep understanding of the problem, visited MIT during 1966-67. I then realized the correspondence between the FUNARG lists which are the results of the evaluation of "closed" Lambda expressions in [[LISP 1.5|LISP]] and [[ISWIM]]'s Lambda Closures. |
|||
the values of <code>a</code> and <code>b</code> 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 <code>x</code> of the enclosing function. The closures in <code>a</code> and <code>b</code> are functionally identical. The only difference in implementation is that in the first case we used a nested function with a name, <code>g</code>, while in the second case we used an anonymous nested function (using the Python keyword <code>lambda</code> for creating an anonymous function). The original name, if any, used in defining them is irrelevant. |
|||
}}</ref><ref>{{cite book |
|||
| author = [[Åke Wikström]] |
|||
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". |
|||
| year = 1987 |
|||
| title = Functional Programming using Standard ML |
|||
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. |
|||
| isbn = 0-13-331968-7 |
|||
| quote = The reason it is called a "closure" is that an expression containing free variables is called an "open" expression, and by associating to it the bindings of its free variables, you close it. |
|||
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 <code>x</code> (bound to the non-local variable <code>x</code> with global scope) are executed in the same environment where <code>x</code> is defined, so it is immaterial whether these are actually closures: |
|||
}}</ref> This usage was subsequently adopted by [[Gerald Jay Sussman|Sussman]] and [[Guy L. Steele, Jr.|Steele]] when they defined [[Scheme (programming language)|Scheme]] in 1975,<ref>{{cite |
|||
<syntaxhighlight lang="python"> |
|||
| author = [[Gerald Jay Sussman]] and [[Guy L. Steele, Jr.]] |
|||
x = 1 |
|||
| month = December |
|||
nums = [1, 2, 3] |
|||
| year = 1975 |
|||
| title = Scheme: An Interpreter for the Extended Lambda Calculus |
|||
def f(y): |
|||
| id = [[AI Memo]] 349 |
|||
return x + y |
|||
}}</ref> and became widespread. |
|||
map(f, nums) |
|||
map(lambda y: x + y, nums) |
|||
</syntaxhighlight> |
|||
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 <code>f</code> can be seen to be a closure because <code>x</code> in the body of <code>f</code> is bound to the <code>x</code> in the global namespace, not the <code>x</code> local to <code>g</code>: |
|||
<syntaxhighlight lang="python"> |
|||
x = 0 |
|||
def f(y): |
|||
return x + y |
|||
def g(z): |
|||
x = 1 # local x shadows global x |
|||
return f(z) |
|||
g(1) # evaluates to 1, not 2 |
|||
</syntaxhighlight> |
|||
== Applications == |
|||
The use of closures is associated with languages where functions are [[first-class object]]s, in which functions can be returned as results from [[higher-order function]]s, or passed as arguments to other function calls; if functions with free variables are first-class, then returning one creates a closure. This includes [[functional programming]] languages such as [[Lisp (programming language)|Lisp]] and [[ML (programming language)|ML]], and many modern, multi-paradigm languages, such as [[Julia (programming language)|Julia]], [[Python (programming language)|Python]], and [[Rust (programming language)|Rust]]. Closures are also often used with [[Callback (computer programming)|callbacks]], particularly for [[event handler]]s, such as in [[JavaScript]], where they are used for interactions with a [[dynamic web page]]. |
|||
Closures can also be used in a [[continuation-passing style]] to [[Information hiding|hide state]]. Constructs such as [[Object (computer science)|object]]s and [[control structure]]s can thus be implemented with closures. In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At [[Run time (program lifecycle phase)|run-time]], when the outer function executes, a closure is formed, consisting of the inner function's code and references (the upvalues) to any variables of the outer function required by the closure. |
|||
== |
=== First-class functions === |
||
{{ |
{{further|First-class function}} |
||
Closures typically appear in languages |
Closures typically appear in languages with [[first-class object|first-class functions]]—in other words, such languages enable functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers. For example, consider the following [[Scheme (programming language)|Scheme]] function: |
||
< |
<syntaxhighlight lang="scheme"> |
||
; Return a list of all books with at least THRESHOLD copies sold. |
; Return a list of all books with at least THRESHOLD copies sold. |
||
(define (best-selling-books threshold) |
(define (best-selling-books threshold) |
||
Line 49: | Line 87: | ||
(lambda (book) |
(lambda (book) |
||
(>= (book-sales book) threshold)) |
(>= (book-sales book) threshold)) |
||
book-list)) |
book-list)) |
||
</syntaxhighlight> |
|||
</source> |
|||
In this example, the [[lambda expression]] <code>(lambda (book) (>= (book-sales book) threshold))</code> appears within the function <code>best-selling-books</code>. When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda expression and a reference to the <code>threshold</code> variable, which is a [[free variable]] inside the lambda expression. |
In this example, the [[Lambda (programming)|lambda expression]] <code>(lambda (book) (>= (book-sales book) threshold))</code> appears within the function <code>best-selling-books</code>. When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda expression and a reference to the <code>threshold</code> variable, which is a [[free variable]] inside the lambda expression. |
||
The closure is then passed to the <code>filter</code> 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 |
The closure is then passed to the <code>filter</code> 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 <code>threshold</code>, it can use that variable each time <code>filter</code> calls it. The function <code>filter</code> might be defined in a separate file. |
||
Here is the same example rewritten in [[JavaScript]], another popular language with support for closures: |
Here is the same example rewritten in [[JavaScript]], another popular language with support for closures: |
||
< |
<syntaxhighlight lang="javascript"> |
||
// Return a list of all books with at least 'threshold' copies sold. |
// Return a list of all books with at least 'threshold' copies sold. |
||
function bestSellingBooks(threshold) { |
function bestSellingBooks(threshold) { |
||
return bookList.filter( |
return bookList.filter(book => book.sales >= threshold); |
||
function (book) { return book.sales >= threshold; } |
|||
); |
|||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
The <code> |
The arrow operator <code>=></code> is used to define an [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow%20functions arrow function expression], and an <code>Array.filter</code> method<ref>{{cite web |url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter |title=array.filter |work=Mozilla Developer Center |date=10 January 2010 |access-date=2010-02-09}}</ref> instead of a global <code>filter</code> 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 |
A function may create a closure and return it, as in this example: |
||
< |
<syntaxhighlight lang="javascript"> |
||
// Return a function that approximates the derivative of f |
// Return a function that approximates the derivative of f |
||
// using an interval of dx, which should be appropriately small. |
// using an interval of dx, which should be appropriately small. |
||
function derivative(f, dx) { |
function derivative(f, dx) { |
||
return |
return x => (f(x + dx) - f(x)) / dx; |
||
return (f(x + dx) - f(x)) / dx; |
|||
}; |
|||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
Because the closure in this case outlives the |
Because the closure in this case outlives the execution of the function that creates it, the variables <code>f</code> and <code>dx</code> live on after the function <code>derivative</code> 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 (computer science)|garbage collection]]. |
||
This is most commonly implemented using some form of [[garbage collection (computer science)|garbage collection]]. |
|||
== |
=== State representation === |
||
A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The [[scope (programming)|scope]] of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. |
A closure can be used to associate a function with a set of "[[Class (computer programming)|private]]" variables, which persist over several invocations of the function. The [[scope (programming)|scope]] of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. These are analogous to [[private variable]]s in [[object-oriented programming]], and in fact closures are similar to stateful [[function objects]] (or functors) with a single call-operator method. |
||
In stateful languages, closures can thus be used to implement paradigms for state representation and [[information hiding]], since the closure's upvalues (its closed-over variables) are of indefinite [[variable (programming)#Scope and extent|extent]], so a value established in one invocation remains available in the next. Closures used in this way no longer have [[ |
In stateful languages, closures can thus be used to implement paradigms for state representation and [[information hiding]], since the closure's upvalues (its closed-over variables) are of indefinite [[variable (programming)#Scope and extent|extent]], so a value established in one invocation remains available in the next. Closures used in this way no longer have [[referential transparency]], and are thus no longer [[pure function]]s; nevertheless, they are commonly used in impure functional languages such as [[Scheme (programming language)|Scheme]]. |
||
== |
=== Other uses === |
||
Closures have many uses: |
Closures have many uses: |
||
* Because closures delay |
* Because closures delay evaluation—i.e., they do not "do" anything until they are called—they can be used to define control structures. For example, all of [[Smalltalk]]'s standard control structures, including branches (if/then/else) and loops (while and for), are defined using objects whose methods accept closures. Users can easily define their own control structures also. |
||
* In languages |
* In languages which implement assignment, multiple functions can be produced that close over the same environment, enabling them to communicate privately by altering that environment. In Scheme: |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define foo #f) |
(define foo #f) |
||
(define bar #f) |
(define bar #f) |
||
Line 105: | Line 138: | ||
(foo "meet me by the docks at midnight") |
(foo "meet me by the docks at midnight") |
||
(display (bar)) ; prints "meet me by the docks at midnight" |
(display (bar)) ; prints "meet me by the docks at midnight" |
||
</syntaxhighlight> |
|||
</source> |
|||
* Closures can be used to implement [[Object-oriented programming|object]] systems.<ref>{{cite web | |
* Closures can be used to implement [[Object-oriented programming|object]] systems.<ref>{{cite web |url=http://okmij.org/ftp/Scheme/oop-in-fp.txt |title=Re: FP, OO and relations. Does anyone trump the others? |date=29 December 1999 |access-date=2008-12-23 |archive-url=https://web.archive.org/web/20081226055307/http://okmij.org/ftp/Scheme/oop-in-fp.txt |archive-date=26 December 2008 |url-status=dead}}</ref> |
||
Note: Some speakers call any data structure that binds a [[Scope (programming)# |
Note: Some speakers call any data structure that binds a [[Scope (programming)#Lexical scoping|lexical]] environment a closure, but the term usually refers specifically to functions. |
||
== Implementation and theory == |
|||
Closures are typically implemented with a special [[data structure]] that contains a [[function pointer|pointer to the function code]], plus a representation of the function's lexical environment (i.e., the set of available variables) at the time when the closure was created. The referencing environment [[name binding|binds]] the non-local names to the corresponding variables in the lexical environment at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure. When the closure is entered at a later time, possibly with a different lexical environment, the function is executed with its non-local variables referring to the ones captured by the closure, not the current environment. |
|||
A language implementation cannot easily support full closures if its run-time memory model allocates all [[automatic variable]]s on a linear [[Stack-based memory allocation|stack]]. In such languages, a function's automatic local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore, those variables must be allocated so that they persist until no longer needed, typically via [[heap allocation]], rather than on the stack, and their lifetime must be managed so they survive until all closures referencing them are no longer in use. |
|||
This explains why, typically, languages that natively support closures also use [[Garbage collection (computer science)|garbage collection]]. The alternatives are manual memory management of non-local variables (explicitly allocating on the heap and freeing when done), or, if using stack allocation, for the language to accept that certain use cases will lead to [[undefined behaviour]], due to [[dangling pointer]]s to freed automatic variables, as in lambda expressions in C++11<ref>''[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2550.pdf Lambda Expressions and Closures]'' C++ Standards Committee. 29 February 2008.</ref> or nested functions in GNU C.<ref>{{cite web |work=GCC Manual |url=https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html |title=6.4 Nested Functions |quote=If you try to call the nested function through its address after the containing function exits, all hell breaks loose. If you try to call it after a containing scope level exits, and if it refers to some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk. If, however, the nested function does not refer to anything that has gone out of scope, you should be safe.}}</ref> The [[funarg problem]] (or "functional argument" problem) describes the difficulty of implementing functions as first class objects in a stack-based programming language such as C or C++. Similarly in [[D (programming language)|D]] version 1, it is assumed that the programmer knows what to do with [[delegation (programming)|delegates]] and automatic local variables, as their references will be invalid after return from its definition scope (automatic local variables are on the stack) – this still permits many useful functional patterns, but for complex cases needs explicit [[heap allocation]] for variables. D version 2 solved this by detecting which variables must be stored on the heap, and performs automatic allocation. Because D uses garbage collection, in both versions, there is no need to track usage of variables as they are passed. |
|||
In strict functional languages with immutable data (''e.g.'' [[Erlang (programming language)|Erlang]]), it is very easy to implement automatic memory management (garbage collection), as there are no possible cycles in variables' references. For example, in Erlang, all arguments and variables are allocated on the heap, but references to them are additionally stored on the stack. After a function returns, references are still valid. Heap cleaning is done by incremental garbage collector. |
|||
In ML, local variables are lexically scoped, and hence define a stack-like model, but since they are bound to values and not to objects, an implementation is free to copy these values into the closure's data structure in a way that is invisible to the programmer. |
|||
[[Scheme (programming language)|Scheme]], which has an [[ALGOL]]-like lexical scope system with dynamic variables and garbage collection, lacks a stack programming model and does not suffer from the limitations of stack-based languages. Closures are expressed naturally in Scheme. The lambda form encloses the code, and the free variables of its environment persist within the program as long as they can possibly be accessed, and so they can be used as freely as any other Scheme expression.{{citation needed|date=December 2014}} |
|||
Closures are closely related to Actors in the [[Actor model]] of concurrent computation where the values in the function's lexical environment are called ''acquaintances''. An important issue for closures in [[concurrent programming]] languages is whether the variables in a closure can be updated and, if so, how these updates can be synchronized. Actors provide one solution.<ref>''[https://dspace.mit.edu/handle/1721.1/6935 Foundations of Actor Semantics]'' Will Clinger. MIT Mathematics Doctoral Dissertation. June 1981.</ref> |
|||
Closures are closely related to [[function object]]s; the transformation from the former to the latter is known as [[defunctionalization]] or [[lambda lifting]]; see also [[closure conversion]].{{Citation needed|date=September 2011}} |
|||
== Differences in semantics == |
|||
==Differences in semantics== |
|||
=== Lexical environment === |
=== Lexical environment === |
||
As different languages do not always have a common definition of the lexical environment, their definitions of closure may vary also. The commonly held minimalist definition of the lexical environment defines it as a set of all [[Name binding|bindings of variables]] in the scope, and that is also what closures in any language have to capture. However the meaning of a [[Variable (programming)|variable]] binding also differs. In imperative languages, variables bind to relative locations in memory that can store values. Although the relative location of a binding does not change at runtime, the value in the bound location can. In such languages, since closure captures the binding, any operation on the variable, whether done from the closure or not, are performed on the same relative memory location. Here is an example illustrating the concept in [[ECMAScript]], which is one such language: |
As different languages do not always have a common definition of the lexical environment, their definitions of closure may vary also. The commonly held minimalist definition of the lexical environment defines it as a set of all [[Name binding|bindings of variables]] in the scope, and that is also what closures in any language have to capture. However the meaning of a [[Variable (programming)|variable]] binding also differs. In imperative languages, variables bind to relative locations in memory that can store values. Although the relative location of a binding does not change at runtime, the value in the bound location can. In such languages, since closure captures the binding, any operation on the variable, whether done from the closure or not, are performed on the same relative memory location. This is often called capturing the variable "by reference". Here is an example illustrating the concept in [[ECMAScript]], which is one such language: |
||
< |
<syntaxhighlight lang="javascript"> |
||
// Javascript |
|||
var f, g; |
var f, g; |
||
function foo() { |
function foo() { |
||
var x |
var x; |
||
f = function() { return ++x; }; |
f = function() { return ++x; }; |
||
g = function() { return --x; }; |
g = function() { return --x; }; |
||
x = 1; |
x = 1; |
||
alert('inside foo, call to f(): ' + f()); |
alert('inside foo, call to f(): ' + f()); |
||
} |
} |
||
foo(); |
foo(); // 2 |
||
alert('call to g(): ' + g()); // |
alert('call to g(): ' + g()); // 1 (--x) |
||
alert('call to |
alert('call to g(): ' + g()); // 0 (--x) |
||
alert('call to f(): ' + f()); // 1 (++x) |
|||
</source> |
|||
alert('call to f(): ' + f()); // 2 (++x) |
|||
Note how function <code>foo</code> and the closures referred to by variables <code>f</code> and <code>g</code> all use the same relative memory location signified by local variable <code>x</code>. |
|||
</syntaxhighlight> |
|||
Function <code>foo</code> and the closures referred to by variables <code>f</code> and <code>g</code> all use the same relative memory location signified by local variable <code>x</code>. |
|||
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 <code>Function.bind()</code>. |
|||
===Example 1: Reference to an unbound variable=== |
|||
<ref>{{cite web |title=Function.prototype.bind() |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind |website=MDN Web Docs |access-date=20 November 2018}}</ref> |
|||
<syntaxhighlight lang="javascript"> |
|||
var module = { |
|||
x: 42, |
|||
getX: function() {return this.x; } |
|||
} |
|||
var unboundGetX = module.getX; |
|||
console.log(unboundGetX()); // The function gets invoked at the global scope |
|||
// emits undefined as 'x' is not specified in global scope. |
|||
var boundGetX = unboundGetX.bind(module); // specify object module as the closure |
|||
console.log(boundGetX()); // emits 42 |
|||
</syntaxhighlight> |
|||
===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.<ref>{{cite web |title=Closures |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#Creating_closures_in_loops_A_common_mistake |website=MDN Web Docs |access-date=20 November 2018}}</ref> |
|||
<syntaxhighlight lang="javascript"> |
|||
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() { |
|||
alert(e.id); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
Again here variable <code>e</code> would need to be bound by the scope of the block using <code>handle.bind(this)</code> or the <code>let</code> keyword. |
|||
On the other hand, many functional languages, such as [[ML (programming language)|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 <code>final</code>, 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 <code>[&]</code>, which means captured by reference, or with <code>[=]</code>, which means captured by value. |
|||
On the other hand, many functional languages, such as [[ML (programming language)|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. |
|||
Yet another subset, [[lazy evaluation|lazy]] functional languages such as [[ |
Yet another subset, [[lazy evaluation|lazy]] functional languages such as [[Haskell]], bind variables to results of future computations rather than values. Consider this example in Haskell: |
||
< |
<syntaxhighlight lang="haskell"> |
||
-- Haskell |
|||
foo :: Num -> Num -> (Num -> Num) |
|||
foo |
foo :: Fractional a => a -> a -> (a -> a) |
||
foo x y = (\z -> z + r) |
|||
where r = x / y |
|||
f :: |
f :: Fractional a => a -> a |
||
f = foo 1 0 |
f = foo 1 0 |
||
main = print (f 123) |
main = print (f 123) |
||
</syntaxhighlight> |
|||
</source> |
|||
The binding of <code>r</code> captured by the closure defined within function <code>foo</code> is to the computation <code>(x / y)</code> |
The binding of <code>r</code> captured by the closure defined within function <code>foo</code> is to the computation <code>(x / y)</code>—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. |
||
=== Closure leaving === |
=== Closure leaving === |
||
Yet more differences manifest themselves in the behavior of other lexically |
Yet more differences manifest themselves in the behavior of other lexically scoped constructs, such as <code>return</code>, <code>break</code> and <code>continue</code> statements. Such constructs can, in general, be considered in terms of invoking an [[escape continuation]] established by an enclosing control statement (in case of <code>break</code> and <code>continue</code>, such interpretation requires looping constructs to be considered in terms of recursive function calls). In some languages, such as ECMAScript, <code>return</code> refers to the continuation established by the closure lexically innermost with respect to the statement—thus, a <code>return</code> within a closure transfers control to the code that called it. However, in [[Smalltalk]], the superficially similar operator <code>^</code> invokes the escape continuation established for the method invocation, ignoring the escape continuations of any intervening nested closures. The escape continuation of a particular closure can only be invoked in Smalltalk implicitly by reaching the end of the closure's code. These examples in ECMAScript and Smalltalk highlight the difference: |
||
< |
<syntaxhighlight lang="smalltalk"> |
||
"Smalltalk" |
"Smalltalk" |
||
foo |
foo |
||
Line 159: | Line 247: | ||
bar |
bar |
||
Transcript show: (self foo printString) "prints 1" |
Transcript show: (self foo printString) "prints 1" |
||
</syntaxhighlight> |
|||
</source> |
|||
< |
<syntaxhighlight lang="javascript"> |
||
// ECMAScript |
// ECMAScript |
||
function foo() { |
function foo() { |
||
Line 169: | Line 257: | ||
} |
} |
||
alert(foo()); // prints 0 |
alert(foo()); // prints 0 |
||
</syntaxhighlight> |
|||
</source> |
|||
The above code snippets will behave differently because the Smalltalk <code>^</code> operator and the JavaScript <code>return</code> operator are not analogous. In the ECMAScript example, <code>return x</code> will leave the inner closure to begin a new iteration of the <code>forEach</code> loop, whereas in the Smalltalk example, <code>^x</code> will abort the loop and return from the method <code>foo</code>. |
The above code snippets will behave differently because the Smalltalk <code>^</code> operator and the JavaScript <code>return</code> operator are not analogous. In the ECMAScript example, <code>return x</code> will leave the inner closure to begin a new iteration of the <code>forEach</code> loop, whereas in the Smalltalk example, <code>^x</code> will abort the loop and return from the method <code>foo</code>. |
||
[[Common Lisp]] provides a construct that can express either of the above actions: |
[[Common Lisp]] provides a construct that can express either of the above actions: Lisp <code>(return-from foo x)</code> behaves as [[Smalltalk]] <code>^x</code>, while Lisp <code>(return-from nil x)</code> behaves as [[JavaScript]] <code>return x</code>. Hence, Smalltalk makes it possible for a captured escape continuation to outlive the extent in which it can be successfully invoked. Consider: |
||
< |
<syntaxhighlight lang="smalltalk"> |
||
"Smalltalk" |
|||
foo |
foo |
||
^[ :x | ^x ] |
^[ :x | ^x ] |
||
Line 182: | Line 271: | ||
f := self foo. |
f := self foo. |
||
f value: 123 "error!" |
f value: 123 "error!" |
||
</syntaxhighlight> |
|||
</source> |
|||
When the closure returned by the method <code>foo</code> is invoked, it attempts to return a value from the invocation of <code>foo</code> 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 (programming language)|Ruby]], enable the programmer to choose the way <code>return</code> is captured. An example in Ruby: |
|||
When the closure returned by the method <code>foo</code> is invoked, it attempts to return a value from the invocation of <code>foo</code> that created the closure. Since that call has already returned and the Smalltalk method invocation model does not follow the [[spaghetti stack]] discipline to allow multiple returns, this operation results in an error. |
|||
<syntaxhighlight lang="ruby"> |
|||
Some languages, such as [[Ruby (programming language)|Ruby]], allow the programmer to choose the way <code>return</code> is captured. An example in Ruby: |
|||
# Ruby |
|||
# Closure using a Proc |
|||
<source lang="ruby"> |
|||
# ruby |
|||
def foo |
def foo |
||
f = Proc.new { return "return from foo from inside proc" } |
f = Proc.new { return "return from foo from inside proc" } |
||
Line 196: | Line 287: | ||
end |
end |
||
# Closure using a lambda |
|||
def bar |
def bar |
||
f = lambda { return "return from lambda" } |
f = lambda { return "return from lambda" } |
||
Line 204: | Line 296: | ||
puts foo # prints "return from foo from inside proc" |
puts foo # prints "return from foo from inside proc" |
||
puts bar # prints "return from bar" |
puts bar # prints "return from bar" |
||
</syntaxhighlight> |
|||
</source> |
|||
Both <code>Proc.new</code> and <code>lambda</code> in this example are ways to create a closure, but semantics of the closures thus created are different with respect to the <code>return</code> statement. |
Both <code>Proc.new</code> and <code>lambda</code> in this example are ways to create a closure, but semantics of the closures thus created are different with respect to the <code>return</code> statement. |
||
Line 210: | Line 302: | ||
In [[Scheme (programming language)|Scheme]], definition and scope of the <code>return</code> 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. |
In [[Scheme (programming language)|Scheme]], definition and scope of the <code>return</code> 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. |
||
< |
<syntaxhighlight lang="Scheme"> |
||
; Scheme |
|||
(define call/cc call-with-current-continuation) |
(define call/cc call-with-current-continuation) |
||
Line 230: | Line 323: | ||
(newline) |
(newline) |
||
(display (bar)) ; prints "return from bar" |
(display (bar)) ; prints "return from bar" |
||
</syntaxhighlight> |
|||
</source> |
|||
== |
== Closure-like constructs == |
||
Some languages have features which simulate the behavior of closures. In languages such as [[C++]], [[C Sharp (programming language)|C#]], [[D (programming language)|D]], [[Java (programming language)|Java]], [[Objective-C]], and [[Visual Basic (.NET)]] (VB.NET), these features are the result of the language's object-oriented paradigm. |
|||
Closures are typically implemented with a special [[data structure]] that contains a pointer to the function code, plus a representation of the function's lexical environment (e.g., the set of available variables and their values) at the time when the closure was created. |
|||
=== Callbacks (C) === |
|||
A language implementation cannot easily support full closures if its run-time memory model allocates all local variables on a linear stack. In such languages, a function's local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore, those variables must be allocated so that they persist until no longer needed. This explains why, typically, languages that natively support closures also use [[Garbage collection (computer science)|garbage collection]]. The alternative is for the language to accept that certain use cases will lead to [[undefined behaviour]], as in the proposal for lambda expressions in C++.<ref>''[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2550.pdf Lambda Expressions and Closures]'' C++ Standards Committee. 29 February 2008.</ref> The [[Funarg problem]] (or "functional argument" problem) describes the difficulty of implementing functions as first class objects in a stack-based programming language such as C or C++. Similarly in [[D (programming language)|D]] version 1, it is assumed that the programmer knows what to do with delegates and local variables, as their references will be invalid after return from its definition scope (local variables are on the stack) - this still permits many useful functional patterns, but for complex cases needs explicit heap allocation for variables. D version 2 solved this by detecting which variables must be stored on the heap, and performs automatic allocation. Because D uses garbage collection, in both versions, there is no need to track usage of variables as they are passed. |
|||
Some [[C (programming language)|C]] libraries support [[Callback (computer programming)|callbacks]]. This is sometimes implemented by providing two values when registering the callback with the library: a function pointer and a separate <code>void*</code> pointer to arbitrary data of the user's choice. When the library executes the callback function, it passes along the data pointer. This enables the callback to maintain state and to refer to information captured at the time it was registered with the library. The idiom is similar to closures in functionality, but not in syntax. The <code>void*</code> pointer is not [[Type safety|type safe]] so this C idiom differs from type-safe closures in C#, Haskell or ML. |
|||
Callbacks are used extensively in [[graphical user interface]] (GUI) [[widget toolkit]]s to implement [[event-driven programming]] by associating general functions of graphical widgets (menus, buttons, check boxes, sliders, spinners, etc.) with application-specific functions implementing the specific desired behavior for the application. |
|||
In strict functional languages with immutable data (''e.g.'' [[Erlang (programming language)|Erlang]]), it is very easy to implement automatic memory management (garbage collection), as there are no possible cycles in variables references. For example in Erlang, all arguments and variables are allocated on the heap, but references to them are additionally stored on the stack. After a function returns, references are still valid. Heap cleaning is done by incremental garbage collector. |
|||
====Nested function and function pointer (C)==== |
|||
In ML, local variables are allocated on a linear stack {{Citation needed|date=July 2010}}. When a closure is created, it copies the values of those variables that are needed by the closure into the closure's data structure. |
|||
With a [[GNU Compiler Collection]] (GCC) extension, a nested function<ref>{{cite web |
|||
|url = https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html |
|||
|title = Nested functions}}</ref> 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 <code>adder</code> is a top-level definition (depending on compiler version, it could produce a correct result if compiled with no optimizing, i.e., at <code>-O0</code>): |
|||
<syntaxhighlight lang="c"> |
|||
[[Scheme (programming language)|Scheme]], which has an [[ALGOL]]-like lexical scope system with dynamic variables and garbage collection, lacks a stack programming model and does not suffer from the limitations of stack-based languages. Closures are expressed naturally in Scheme. The lambda form encloses the code and the free variables of its environment, persists within the program as long as it can possibly be accessed, and can be used as freely as any other Scheme expression. |
|||
#include <stdio.h> |
|||
typedef int (*fn_int_to_int)(int); // type of function int->int |
|||
Closures are closely related to Actors in the [[Actor model]] of concurrent computation where the values in the function's lexical environment are called ''acquaintances''. An important issue for closures in [[concurrent programming]] languages is whether the variables in a closure can be updated and, if so, how these updates can be synchronized. Actors provide one solution.<ref>''[https://dspace.mit.edu/handle/1721.1/6935 Foundations of Actor Semantics]'' Will Clinger. MIT Mathematics Doctoral Dissertation. June 1981.</ref> |
|||
fn_int_to_int adder(int number) { |
|||
==Closure-like constructs in other languages== |
|||
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 |
|||
In [[C (programming language)|C]], libraries that support [[callback (computer science)|callback]]s sometimes allow a callback to be registered using two values: a function pointer and a separate <code>void*</code> pointer to arbitrary data of the user's choice. Each time the library executes the callback function, it passes in the data pointer. This allows the callback to maintain state and to refer to information captured at the time it was registered. The idiom is similar to closures in functionality, but not in syntax. |
|||
Several object-oriented techniques and language features simulate some features of closures. For example: |
|||
===Eiffel=== |
|||
[[Eiffel (programming language)|Eiffel]] includes '''inline agents''' defining closures. An inline agent is an object representing a routine, defined by giving the code of the routine in-line. For example, in |
|||
<source lang="eiffel"> |
|||
ok_button.click_event.subscribe ( |
|||
agent (x, y: INTEGER) do |
|||
map.country_at_coordinates (x, y).display |
|||
end |
|||
) |
|||
</source> |
|||
the argument to <code>subscribe</code> 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 <code>click_event</code> 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 true closures, is that they cannot reference local variables from enclosing scope, but this can easily be worked around by providing additional closed operands to the agent. Only <tt>Current</tt> (a reference to current object, analogous to <tt>this</tt> in Java), its features, and arguments of the agent itself can be accessed from within the agent body. |
|||
===Erlang=== |
|||
In [[Erlang (programming language)|Erlang]], closures are supported simply using the keyword <code>fun</code> (Erlang's name for anonymous function) with references to outer variables. Because Erlang is a functional language with immutable value passing semantics, it is both easy to construct closures, execute them, or manage memory. Implementation is done by hidden module-level functions with N+M arguments (N: number of closed outer variables; M: number of own arguments), which is also very simple (see [[Lambda lifting]]). |
|||
<source lang="erlang"> |
|||
construct_filter(L) -> |
|||
Filter = fun (X) -> lists:member(X, L) end, % by using L in this fun, |
|||
Filter. % we construct closure |
|||
complex_filter(SmallListOfSearchedElements, BigListToBeSearched) -> |
|||
Filter = construct_filter(SmallListOfSearchedElements), |
|||
Result = lists:filter(Filter, BigListToBeSearched), |
|||
Result. |
|||
</source> |
|||
===C++ (operator overloading)=== |
|||
[[C++]] allows defining [[function object]]s by overloading <code>operator()</code>. These objects behave somewhat like functions in a functional programming language. They may be created at runtime and may contain state, but they do not implicitly capture local variables as closures do. Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.<ref>[http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2006/#mailing2006-02 JTC1/SC22/WG21 - Papers 2006 mailing2006-02-pre-Berlin]</ref><ref>[http://val.samko.info/lambda/ A proposal to add lambda functions to the C++ standard]</ref> The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write |
|||
<source lang="cpp"> |
|||
void foo(string myname) { |
|||
typedef vector<string> names; |
|||
int y; |
|||
names n; |
|||
// ... |
|||
names::iterator i = std::find_if(n.begin(), n.end(), [&](const string& s) { |
|||
return s != myname && s.size() > y; |
|||
}); |
|||
// 'i' is now either 'n.end()' or points to the first string in 'n' |
|||
// which is not equal to 'myname' and whose length is greater than 'y' |
|||
} |
} |
||
</source> |
|||
At least two C++ compilers, Visual C++ 2010 and [[GNU Compiler Collection|GCC]] 4.5, already support this notation. |
|||
int main(void) { |
|||
===C#=== |
|||
fn_int_to_int add10 = adder(10); |
|||
printf("%d\n", add10(1)); |
|||
C# anonymous methods and lambda expressions support closure to local variables: |
|||
return 0; |
|||
<source lang="csharp"> |
|||
var data = new[] {1, 2, 3, 4}; |
|||
var multiplier = 2; |
|||
var result = data.Select(x => x * multiplier); |
|||
</source> |
|||
===D=== |
|||
Closures are implemented by delegates in [[D (programming language)|D]]. |
|||
<source lang="D"> |
|||
auto test1() { |
|||
int a = 7; |
|||
return delegate() { return a + 3; }; // anonymous delegate construction |
|||
} |
} |
||
</syntaxhighlight> |
|||
But moving <code>adder</code> (and, optionally, the <code>typedef</code>) in <code>main</code> makes it valid: |
|||
auto test2() { |
|||
int a = 20; |
|||
int foo() { return a + 5; } // inner function |
|||
return &foo; // other way to constructing delegate |
|||
} |
|||
<syntaxhighlight lang="c"> |
|||
void bar() { |
|||
#include <stdio.h> |
|||
auto dg = test1(); |
|||
dg(); // =10 // ok, test1.a is in a closure and still exists |
|||
int main(void) { |
|||
dg = test2(); |
|||
typedef int (*fn_int_to_int)(int); // type of function int->int |
|||
dg(); // =25 // ok, test2.a is in a closure and still exists |
|||
fn_int_to_int adder(int number) { |
|||
int add (int value) { return value + number; } |
|||
return add; |
|||
} |
|||
fn_int_to_int add10 = adder(10); |
|||
printf("%d\n", add10(1)); |
|||
return 0; |
|||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
If executed this now prints <code>11</code> as expected. |
|||
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 often not an issue in practice. |
|||
=== Local classes and lambda functions (Java) === |
|||
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 is allowed to escapes 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 don't 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 references a function's variables. |
|||
[[Java (programming language)|Java]] enables [[class (object-oriented programming)|classes]] to be defined inside [[method (object-oriented programming)|methods]]. These are called ''local classes''. When such classes are not named, they are known as ''[[anonymous class]]es'' (or anonymous ''inner'' classes). A local class (either named or anonymous) may refer to names in lexically enclosing classes, or read-only variables (marked as <code>final</code>) in the lexically enclosing method. |
|||
<syntaxhighlight lang="java"> |
|||
===Delphi=== |
|||
class CalculationWindow extends JFrame { |
|||
private volatile int result; |
|||
[[Embarcadero Delphi|Delphi]] includes support for closures implemented as [[anonymous functions|anonymous methods]] since the 2009 release. They can be declared inline within another method, and capture references to local state. For example: |
|||
// ... |
|||
public void calculateInSeparateThread(final URI uri) { |
|||
<source lang="delphi"> |
|||
// The expression "new Runnable() { ... }" is an anonymous class implementing the 'Runnable' interface. |
|||
type |
|||
new Thread( |
|||
TCounter = reference to function: integer; |
|||
new Runnable() { |
|||
void run() { |
|||
function MakeCounter: TCounter; |
|||
// It can read final local variables: |
|||
var |
|||
calculate(uri); |
|||
i: integer; |
|||
// It can access private fields of the enclosing class: |
|||
begin |
|||
result = result + 10; |
|||
i := 0; |
|||
} |
|||
result := function: integer |
|||
} |
|||
).start(); |
|||
} |
|||
result := i; |
|||
end; |
|||
end; |
|||
</source> |
|||
Calling MakeCounter will return a new anonymous method. Invoking it will return 1 the first time, 2 the second, and so on. |
|||
===Perl=== |
|||
[[Anonymous functions]] in [[Perl]] become closures when they reference lexical variables defined in enclosing scopes. |
|||
<source lang="perl"> |
|||
sub number_of_bagels { |
|||
my $bagels = shift; |
|||
return sub { my $sub_arg = shift; return $sub_arg + $bagels; }; |
|||
} |
} |
||
</syntaxhighlight> |
|||
The capturing of <code>final</code> variables enables capturing variables by value. Even if the variable to capture is non-<code>final</code>, it can always be copied to a temporary <code>final</code> variable just before the class. |
|||
my $func = number_of_bagels(5); |
|||
print "I now have " . $func->(2) . " bagels!\n"; #I now have 7 bagels! |
|||
print "I now have " . $func->(10) . " bagels!\n"; #I now have 15 bagels! |
|||
</source> |
|||
===PHP=== |
|||
Since PHP 5.3+<ref>''[http://www.php.net/manual/en/functions.anonymous.php PHP Manual: Anonymous functions.]''</ref>, closures can be implemented as anonymous functions: |
|||
<source lang="php"> |
|||
$greet = function($name) { |
|||
printf("Hello %s\r\n", $name); |
|||
}; |
|||
$greet('World'); |
|||
$greet('PHP'); |
|||
</source> |
|||
Or they can be implemented truer to the closure definition by using the <code>use</code> keyword which makes the calling environment available: |
|||
<source lang="php"> |
|||
$foo = function() use (&$bar) { |
|||
echo($bar); |
|||
}; |
|||
$bar = "Hello, World!"; |
|||
$foo(); |
|||
</source> |
|||
===Python=== |
|||
[[Python (computer language)|Python]] has two types of closures: [[lambda expression]]s and named closures (functions declared inside of functions). |
|||
Lambda expressions look like ''lambda x: x+3''. A two-argument equivalent is ''lambda x,y: x*y'' and a no-argument equivalent is ''lambda: foo()''. These are limited by the fact that the result of the lambda expression must be one expression rather than a statement. Thus, it is not possible to put a "for", "while", or "if" block in a lambda expression. (However, "for" loops can be simulated using a [[list comprehension]], e.g. ''lambda x: [foo(y) for y in x]'', and "if" blocks can be simulated using conditional expressions, e.g. ''lambda x: foo(x) if x == 2 else bar(x)''.) The two can be combined using "if" modifiers on list comprehensions, e.g. ''lambda x: [foo(y) for y in x if y > 5]''. |
|||
The other type of closure is named closures, as shown in the following Python 3.1 example: |
|||
<source lang="python"> |
|||
#tested on python 3.1 |
|||
def outer(): |
|||
y = 0 |
|||
def inner(): |
|||
nonlocal y |
|||
y += 1 |
|||
return y |
|||
return inner |
|||
f = outer() |
|||
print(f(), f(), f()) #prints 1 2 3 |
|||
</source> |
|||
The nested function declaration ''def inner()'' actually declares a closure and assigns it the name of "inner". These nested functions have the full power of closures, i.e. the lexical environment at the time the "def" expression is evaluated is captured and can be referenced or assigned to even after the enclosing function goes out of scope, as shown. |
|||
In Python, a ''def'' statement is like a special assignment statement for functions—just like the assignment ''x = y'' binds ''x'' to the value ''y'', ''def foo(): body'' binds the name ''foo'' to the code ''body''.<ref>http://docs.python.org/reference/compound_stmts.html#function-definitions</ref> The best way to conceive of how a nested function declaration works is to consider it to be equivalent to the creation of a closure followed by its assignment to a variable. For example, the following is what the above function might look like in a hypothetical extended Python language where lambda expressions are allowed to contain a full statement block: |
|||
Capturing of variables by reference can be emulated by using a <code>final</code> 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. |
|||
<source lang="python"> |
|||
# NOTE: The following is NOT valid Python code! |
|||
# It is pseudo-code using a Python-like syntax. |
|||
With the advent of Java 8's lambda expressions,<ref>{{cite web |url=http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html |title=Lambda Expressions |work=The Java Tutorials}}</ref> the closure causes the above code to be executed as: |
|||
def outer(): |
|||
y = 0 |
|||
inner = lambda: ( |
|||
nonlocal y |
|||
y += 1 |
|||
return y |
|||
) |
|||
return inner |
|||
<syntaxhighlight lang="java"> |
|||
f = outer() |
|||
print(f(), f(), f()) #prints 1 2 3 |
|||
</source> |
|||
===Java=== |
|||
[[Java (programming language)|Java]] allows defining "anonymous [[class (object-oriented programming)|classes]]" inside a [[method (object-oriented programming)|method]]; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as <tt>final</tt>) in the lexically enclosing method. |
|||
<source lang="java"> |
|||
class CalculationWindow extends JFrame { |
class CalculationWindow extends JFrame { |
||
private volatile int result; |
|||
// ... |
|||
public void calculateInSeparateThread(final URI uri) { |
|||
// The code () -> { /* code */ } is a closure. |
|||
new Thread(() -> { |
|||
calculate(uri); |
|||
new Runnable() { |
|||
result = result + 10; |
|||
void run() { |
|||
}).start(); |
|||
// It can read final local variables: |
|||
} |
|||
calculate(uri); |
|||
// It can access private fields of the enclosing class: |
|||
result = result + 10; |
|||
} |
|||
} |
|||
).start(); |
|||
} |
|||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
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.<ref> |
|||
Some features of full closures can be emulated by using a <tt>final</tt> reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. |
|||
{{cite web |url=https://blogs.oracle.com/darcy/entry/nested_inner_member_and_top |
|||
|title=Nested, Inner, Member, and Top-Level Classes |work=Joseph D. Darcy's Oracle Weblog |date=July 2007|archive-url=https://web.archive.org/web/20160831172734/https://blogs.oracle.com/darcy/entry/nested_inner_member_and_top |archive-date=31 August 2016 }}</ref> They are normally referred to just as "inner classes".<ref> |
|||
{{cite web |url=https://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html |
|||
|title=Inner Class Example |work=The Java Tutorials: Learning the Java Language: Classes and Objects |
|||
}}</ref> 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.<ref> |
|||
{{cite web |url=https://java.sun.com/docs/books/tutorial/java/javaOO/nested.html |
|||
|title=Nested Classes |work=The Java Tutorials: Learning the Java Language: Classes and Objects |
|||
}}</ref> |
|||
<syntaxhighlight lang="java"> |
|||
According to a Java 7 proposal,<ref>{{cite web |
|||
| url = http://mail.openjdk.java.net/pipermail/lambda-dev/attachments/20100212/af8d2cc5/attachment-0001.txt |
|||
| title = Java 7 Closure Draft Specification |
|||
}}</ref> closures will allow the above code to be executed as: |
|||
<source lang="java"> |
|||
class CalculationWindow extends JFrame { |
|||
private volatile int result; |
|||
... |
|||
public void calculateInSeparateThread(final URI uri) { |
|||
// the code #(){ /* code */ } is a closure |
|||
new Thread(#(){ |
|||
calculate( uri ); |
|||
result = result + 10; |
|||
}).start(); |
|||
} |
|||
} |
|||
</source> |
|||
Java also supports another form of classes, which are called inner (or nested) classes.<ref>{{cite web |
|||
| url = http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html |
|||
| title = Nested Classes (The Java Tutorials > Learning the Java Language > Classes and Objects) |
|||
}}</ref><ref>{{cite web |
|||
| url = http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html |
|||
| title = Inner Class Example (The Java Tutorials > Learning the Java Language > Classes and Objects) |
|||
}}</ref> These are defined in the body of an enclosing class and have full access to each and every instance variable of the enclosing class, thus resembling standard function closures. Due to their binding to these instance variables, a nested inner class may only be instantiated with an explicit binding to an instance of the enclosing class using a special syntax. |
|||
<source lang="java"> |
|||
public class EnclosingClass { |
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; |
|||
counter = 0; |
|||
} |
|||
} |
|||
public static void main(String[] args) { |
|||
EnclosingClass enclosingClassInstance = new EnclosingClass(); |
|||
return counter; |
|||
/* Instantiate the inner class, with binding to the instance */ |
|||
} |
|||
EnclosingClass.InnerClass innerClassInstance = |
|||
enclosingClassInstance.new InnerClass(); |
|||
for (int i = enclosingClassInstance.getCounter(); |
|||
public static void main(String[] args) { |
|||
(i = innerClassInstance.incrementAndReturnCounter()) < 10; |
|||
EnclosingClass enclosingClassInstance = new EnclosingClass(); |
|||
/* increment step omitted */) { |
|||
/* Instantiate the inner class, with binding to the instance */ |
|||
System.out.println(i); |
|||
EnclosingClass.InnerClass innerClassInstance = |
|||
} |
|||
enclosingClassInstance.new InnerClass(); |
|||
} |
|||
for(int i = enclosingClassInstance.getCounter(); (i = |
|||
innerClassInstance.incrementAndReturnCounter()) < 10;) { |
|||
System.out.println(i); |
|||
} |
|||
} |
|||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
Upon execution, this will print the integers from 0 to 9. Beware to not confuse this type of class with the |
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 version history#Java_8|Java 8]], Java supports functions as first class objects. Lambda expressions of this form are considered of type <code>Function<T,U></code> with T being the domain and U the image type. The expression can be called with its <code>.apply(T t)</code> method, but not with a standard method call. |
|||
There have been a number of proposals for adding more fully featured closures to Java.<ref>http://www.javac.info/</ref><ref>http://docs.google.com/View?docid=k73_1ggr36h</ref><ref>http://docs.google.com/Doc?id=ddhp95vd_0f7mcns</ref> |
|||
<syntaxhighlight lang="java"> |
|||
=== Lua === |
|||
public static void main(String[] args) { |
|||
The [[Lua (programming language)|Lua]] programming language provides fully featured closures <ref>{{cite web|title=Programming in Lua : 6.1|url=http://www.lua.org/pil/6.1.html}}</ref>. In combination with functions, which are first-class values in Lua, one may write generators (functions that create functions) as shown in the following example: |
|||
Function<String, Integer> length = s -> s.length(); |
|||
System.out.println( length.apply("Hello, world!") ); // Will print 13. |
|||
<source lang="Lua"> |
|||
} |
|||
function newCounter () |
|||
</syntaxhighlight> |
|||
local i = 0 |
|||
return function () -- anonymous function |
|||
i = i + 1 |
|||
return i |
|||
end |
|||
end |
|||
c1 = newCounter() |
|||
print(c1()) --> 1 |
|||
print(c1()) --> 2 |
|||
</source> |
|||
=== C, C++, Objective-C 2.0 |
=== Blocks (C, C++, Objective-C 2.0) === |
||
{{Main|Blocks (C language extension)}} |
|||
[[Apple Inc.|Apple]] introduced [[Blocks (C language extension)|blocks]], a form of closure, as a nonstandard extension into [[C (programming language)|C]], [[C++]], [[Objective-C 2.0]] and in [[Mac OS X Snow Leopard|Mac OS X 10.6 "Snow Leopard"]] and [[iOS 4]].0. Apple made their implementation available for the GCC and clang compilers. |
|||
Pointers to block and block literals are marked with <code>^</code>. Normal local variables are captured by value when the block is created, and are read-only inside the block. Variables to be captured by reference are marked with <code>__block</code>. Blocks that need to persist outside of the scope they are created in may need to be copied.<ref>{{cite web |url=https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html |title=Blocks Programming Topics |author=<!-- Unstated staff writer --> |date=8 March 2011 |publisher=Apple Inc. |access-date=2011-03-08}}</ref><ref>{{cite web |url=http://thirdcog.eu/pwcblocks/ |title=Programming with C Blocks on Apple Devices |last=Bengtsson |first=Joachim |date=7 July 2010|access-date=2010-09-18|archive-url=https://web.archive.org/web/20101025034928/http://thirdcog.eu/pwcblocks/|archive-date=25 October 2010|url-status=dead}}</ref> |
|||
< |
<syntaxhighlight lang="objc"> |
||
typedef int (^IntBlock)(); |
typedef int (^IntBlock)(); |
||
Line 568: | Line 494: | ||
NSLog(@"%d", f()); |
NSLog(@"%d", f()); |
||
NSLog(@"%d", f()); |
NSLog(@"%d", f()); |
||
</syntaxhighlight> |
|||
</source> |
|||
=== |
=== Delegates (C#, VB.NET, D) === |
||
[[C Sharp (programming language)|C#]] anonymous methods and lambda expressions support closure: |
|||
[[JavaScript]] features closures also. |
|||
<source lang="javascript"> |
|||
<syntaxhighlight lang="csharp"> |
|||
function countDownFrom(count) { |
|||
var data = new[] {1, 2, 3, 4}; |
|||
return function() { |
|||
var multiplier = 2; |
|||
return count--; |
|||
var result = data.Select(x => x * multiplier); |
|||
}; |
|||
</syntaxhighlight> |
|||
[[Visual Basic .NET]], which has many language features similar to those of C#, also supports lambda expressions with closures: |
|||
<syntaxhighlight lang="vb.net"> |
|||
Dim data = {1, 2, 3, 4} |
|||
Dim multiplier = 2 |
|||
Dim result = data.Select(Function(x) x * multiplier) |
|||
</syntaxhighlight> |
|||
In [[D (programming language)|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). |
|||
<syntaxhighlight lang="D"> |
|||
auto test1() { |
|||
int a = 7; |
|||
return delegate() { return a + 3; }; // anonymous delegate construction |
|||
} |
} |
||
auto test2() { |
|||
var countDownFromOne = countDownFrom(1); |
|||
int a = 20; |
|||
var countDownFromTwo = countDownFrom(2); |
|||
int foo() { return a + 5; } // inner function |
|||
return &foo; // other way to construct delegate |
|||
} |
|||
void bar() { |
|||
// example uses console |
|||
auto dg = test1(); |
|||
// which may not be available depending on browser/environment; |
|||
dg(); // =10 // ok, test1.a is in a closure and still exists |
|||
// substitute accordingly (e.g.: with alert). |
|||
dg = test2(); |
|||
console.log(countDownFromOne()); //>1 |
|||
dg(); // =25 // ok, test2.a is in a closure and still exists |
|||
console.log(countDownFromTwo()); //>2 |
|||
} |
|||
console.log(countDownFromOne()); //>0 |
|||
</syntaxhighlight> |
|||
console.log(countDownFromTwo()); //>1 |
|||
console.log(countDownFromTwo()); //>0 |
|||
</source> |
|||
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. |
|||
=== Go === |
|||
In [[Go (programming language)|Go]] a function literal represents an anonymous function, so a function literal <i>is</i> a closure.<ref>{{cite web|url=http://golang.org/doc/codewalk/functions/|title=Codewalk: First-Class Functions in Go|author=The Go Authors|accessdate=2011-09-08}}</ref> |
|||
<source lang="go"> |
|||
// A strategy chooses an action for any given score. |
|||
type strategy func(score) action |
|||
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. |
|||
// stayAtK returns a strategy that rolls until thisTurn is at least k, then stays. |
|||
func stayAtK(k int) strategy { |
|||
=== Function objects (C++) === |
|||
return func(s score) action { |
|||
[[C++]] enables defining [[function object]]s by overloading <code>operator()</code>. These objects behave somewhat like functions in a functional programming language. They may be created at runtime and may contain state, but they do not implicitly capture local variables as closures do. As of [[C++11|the 2011 revision]], the C++ language also supports closures, which are a type of function object constructed automatically from a special language construct called ''lambda-expression''. A C++ closure may capture its context either by storing copies of the accessed variables as members of the closure object or by reference. In the latter case, if the closure object escapes the scope of a referenced object, invoking its <code>operator()</code> causes undefined behavior since C++ closures do not extend the lifetime of their context.{{main|Anonymous function#C++ (since C++11)}} |
|||
if s.thisTurn >= k { |
|||
return stay |
|||
<syntaxhighlight lang="cpp"> |
|||
} |
|||
void foo(string myname) { |
|||
return roll |
|||
int y; |
|||
} |
|||
vector<string> n; |
|||
// ... |
|||
auto i = std::find_if(n.begin(), n.end(), |
|||
// this is the lambda expression: |
|||
[&](const string& s) { return s != myname && s.size() > y; } |
|||
); |
|||
// 'i' is now either 'n.end()' or points to the first string in 'n' |
|||
// which is not equal to 'myname' and whose length is greater than 'y' |
|||
} |
} |
||
</syntaxhighlight> |
|||
</source> |
|||
=== Inline agents (Eiffel) === |
|||
[[Eiffel (programming language)|Eiffel]] includes inline agents defining closures. An inline agent is an object representing a routine, defined by giving the code of the routine in-line. For example, in |
|||
<syntaxhighlight lang="eiffel"> |
|||
ok_button.click_event.subscribe ( |
|||
agent (x, y: INTEGER) do |
|||
map.country_at_coordinates (x, y).display |
|||
end |
|||
) |
|||
</syntaxhighlight> |
|||
the argument to <code>subscribe</code> 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 <code>click_event</code> 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 <code>x</code> and <code>y</code>. |
|||
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 <code>Current</code> (a reference to current object, analogous to <code>this</code> 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. |
|||
=== C++Builder __closure reserved word === |
|||
Embarcadero C++Builder provides the reserved word <code>__closure</code> to provide a pointer to a method with a similar syntax to a function pointer.<ref>Full documentation can be found at http://docwiki.embarcadero.com/RADStudio/Rio/en/Closure</ref> |
|||
Standard C allows writing a {{mono|[[typedef]]}} for a pointer to a [[function type]] using the following syntax:<syntaxhighlight lang="c++"> |
|||
typedef void (*TMyFunctionPointer)( void ); |
|||
</syntaxhighlight>In a similar way, a {{mono|typedef}} can be declared for a pointer to a method using this syntax:<syntaxhighlight lang="c++"> |
|||
typedef void (__closure *TMyMethodPointer)(); |
|||
</syntaxhighlight> |
|||
==See also== |
== See also == |
||
{{div col}} |
|||
* [[Anonymous function]] |
|||
* [[Blocks (C language extension)]] |
|||
* [[Command pattern]] |
* [[Command pattern]] |
||
* [[Continuation]] |
|||
* [[Currying]] |
* [[Currying]] |
||
* [[Funarg problem]] |
|||
* [[Lambda calculus]] |
* [[Lambda calculus]] |
||
* [[Lazy evaluation]] |
|||
* [[Partial application]] |
* [[Partial application]] |
||
* [[Spaghetti stack]] |
|||
* [[Syntactic closure]] |
* [[Syntactic closure]] |
||
* [[Value-level programming]] |
* [[Value-level programming]] |
||
{{div col end}} |
|||
== |
== Notes == |
||
{{ |
{{Notelist}} |
||
== |
== References == |
||
{{Reflist|30em}} |
|||
*[http://library.readscheme.org/page1.html The Original "Lambda Papers"]: A classic series of papers by [[Guy Steele]] and [[Gerald Sussman]] discussing, among other things, the versatility of closures in the context of Scheme (where they appear as ''[[lambda calculus|lambda]] expressions''). |
|||
== External links == |
|||
*[https://web.archive.org/web/20160510140804/http://library.readscheme.org/page1.html Original "Lambda Papers"]: A classic series of papers by [[Guy L. Steele Jr.]] and [[Gerald Jay Sussman]] discussing, among other things, the versatility of closures in the context of Scheme (where they appear as ''[[lambda calculus|lambda]] expressions''). |
|||
* {{cite web |
* {{cite web |
||
|last=Gafter |first=Neal |
|||
| author = [[Neal Gafter]] |
|||
| |
|date=2007-01-28 |
||
| |
|title=A Definition of Closures |
||
| |
|url=http://gafter.blogspot.com/2007/01/definition-of-closures.html |
||
}} |
}} |
||
* {{cite web |
* {{cite web |
||
| |
|last1=Bracha |first1=Gilad |author1-link=Gilad Bracha |last2=Gafter |first2=Neal |last3=Gosling |first3=James |author3-link=James Gosling |last4=von der Ahé |first4=Peter |
||
| |
|title=Closures for the Java Programming Language (v0.5) |
||
| |
|url=http://www.javac.info/closures-v05.html |
||
}} |
}} |
||
*[http://martinfowler.com/bliki/Closure.html Closures]: An article about closures in [[ |
*[http://martinfowler.com/bliki/Closure.html Closures]: An article about closures in [[Dynamic typing|dynamically typed]] imperative languages, by [[Martin Fowler (software engineer)|Martin Fowler]]. |
||
*[http://martinfowler.com/bliki/CollectionClosureMethod.html Collection closure methods]: An example of a technical domain where using closures is convenient, by |
*[http://martinfowler.com/bliki/CollectionClosureMethod.html Collection closure methods]: An example of a technical domain where using closures is convenient, by Martin Fowler. |
||
;Javascript |
|||
*[http://blogs.msdn.com/kartikb/archive/2009/02/08/closures.aspx What are closures]: A post on closures in Javascript. |
|||
*[http://blog.morrisjohns.com/javascript_closures_for_dummies.html Javascript Closures for Dummies]: An article teaching closures in Javascript by examples. |
|||
;Java and .NET |
|||
*[http://www.developerfusion.com/article/8251/the-beauty-of-closures/ The beauty of closures]: An article about using closures in Java and .NET |
|||
;Delphi |
|||
* Nick Hodges, "[http://dn.codegear.com/article/38757 Delphi 2009 Reviewers Guide]", October 2008, ''CodeGear Developer Network'', CodeGear. |
|||
* Craig Stuntz, "[http://blogs.teamb.com/craigstuntz/2008/08/04/37828/ Understanding Anonymous Methods]", October 2008 |
|||
* Dr. Bob, "[http://www.drbob42.com/examines/examinA5.htm Delphi 2009 Anonymous Methods]" |
|||
;Ruby |
|||
* Robert Sosinski, "[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Understanding Ruby Blocks, Procs and Lambdas]", December 2008 |
|||
{{Use dmy dates|date=February 2011}} |
|||
{{DEFAULTSORT:Closure (Computer Science)}} |
{{DEFAULTSORT:Closure (Computer Science)}} |
||
Line 659: | Line 618: | ||
[[Category:Implementation of functional programming languages]] |
[[Category:Implementation of functional programming languages]] |
||
[[Category:Subroutines]] |
[[Category:Subroutines]] |
||
[[Category:Articles with example C++ code]] |
|||
[[Category:Articles with example C Sharp code]] |
|||
[[bg:Затваряне (информатика)]] |
|||
[[Category:Articles with example D code]] |
|||
[[de:Closure]] |
|||
[[Category:Articles with example Eiffel code]] |
|||
[[et:Sulund (informaatika)]] |
|||
[[Category:Articles with example Haskell code]] |
|||
[[el:Κλείσιμο (επιστήμη υπολογιστών)]] |
|||
[[Category:Articles with example Java code]] |
|||
[[es:Clausura (informática)]] |
|||
[[Category:Articles with example JavaScript code]] |
|||
[[fr:Fermeture (informatique)]] |
|||
[[Category:Articles with example Objective-C code]] |
|||
[[it:Chiusura (informatica)]] |
|||
[[Category:Articles with example Python (programming language) code]] |
|||
[[nl:Closure]] |
|||
[[Category:Articles with example Ruby code]] |
|||
[[ja:クロージャ]] |
|||
[[Category:Articles with example Scheme (programming language) code]] |
|||
[[pl:Domknięcie (programowanie)]] |
|||
[[Category:Articles with example Smalltalk code]] |
|||
[[pt:Closure]] |
|||
[[ru:Замыкание (программирование)]] |
|||
[[simple:Closure (computer science)]] |
|||
[[th:ส่วนปิดคลุม (วิทยาการคอมพิวเตอร์)]] |
|||
[[uk:Замикання (програмування)]] |
|||
[[zh:闭包 (计算机科学)]] |
Latest revision as of 04:58, 30 December 2024
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[a] together with an environment.[1] 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.[b] 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.
History and etymology
[edit]The concept of closures was developed in the 1960s for the mechanical evaluation of expressions in the λ-calculus and was first fully implemented in 1970 as a language feature in the PAL programming language to support lexically scoped first-class functions.[2]
Peter Landin defined the term closure in 1964 as having an environment part and a control part as used by his SECD machine for evaluating expressions.[3] Joel Moses credits Landin with introducing the term closure to refer to a lambda expression with open bindings (free variables) that have been closed by (or bound in) the lexical environment, resulting in a closed expression, or closure.[4][5] This use was subsequently adopted by Sussman and Steele when they defined Scheme in 1975,[6] a lexically scoped variant of Lisp, and became widespread.
Sussman and Abelson also use the term closure in the 1980s with a second, unrelated meaning: the property of an operator that adds data to a data structure to also be able to add nested data structures. This use of the term comes from mathematics use, rather than the prior use in computer science. The authors consider this overlap in terminology to be "unfortunate."[7]
Anonymous functions
[edit]The term closure is often used as a synonym for anonymous function, though strictly, an anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations (depending on the language; see the lexical environment section below).
For example, in the following Python code:
def f(x):
def g(y):
return x + y
return g # Return a closure.
def h(x):
return lambda y: x + y # Return a closure.
# Assigning specific closures to variables.
a = f(1)
b = 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 = 1
nums = [1, 2, 3]
def f(y):
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 = 0
def f(y):
return x + y
def g(z):
x = 1 # local x shadows global x
return f(z)
g(1) # evaluates to 1, not 2
Applications
[edit]The use of closures is associated with languages where functions are first-class objects, in which functions can be returned as results from higher-order functions, or passed as arguments to other function calls; if functions with free variables are first-class, then returning one creates a closure. This includes functional programming languages such as Lisp and ML, and many modern, multi-paradigm languages, such as Julia, Python, and Rust. Closures are also often used with callbacks, particularly for event handlers, such as in JavaScript, where they are used for interactions with a dynamic web page.
Closures can also be used in a continuation-passing style to hide state. Constructs such as objects and control structures can thus be implemented with closures. In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At run-time, when the outer function executes, a closure is formed, consisting of the inner function's code and references (the upvalues) to any variables of the outer function required by the closure.
First-class functions
[edit]Closures typically appear in languages with first-class functions—in other words, such languages enable functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers. For example, consider the following Scheme function:
; 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 JavaScript, another popular language with support for closures:
// Return a list of all books with at least 'threshold' copies sold.
function bestSellingBooks(threshold) {
return bookList.filter(book => book.sales >= threshold);
}
The arrow operator =>
is used to define an arrow function expression, and an Array.filter
method[8] 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:
// Return a function that approximates the derivative of f
// using an interval of dx, which should be appropriately small.
function derivative(f, dx) {
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.
State representation
[edit]A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. These are analogous to private variables in object-oriented programming, and in fact closures are similar to stateful function objects (or functors) with a single call-operator method.
In stateful languages, closures can thus be used to implement paradigms for state representation and information hiding, since the closure's upvalues (its closed-over variables) are of indefinite extent, so a value established in one invocation remains available in the next. Closures used in this way no longer have referential transparency, and are thus no longer pure functions; nevertheless, they are commonly used in impure functional languages such as Scheme.
Other uses
[edit]Closures have many uses:
- Because closures delay evaluation—i.e., they do not "do" anything until they are called—they can be used to define control structures. For example, all of Smalltalk's standard control structures, including branches (if/then/else) and loops (while and for), are defined using objects whose methods accept closures. Users can easily define their own control structures also.
- In languages which implement assignment, multiple functions can be produced that close over the same environment, enabling them to communicate privately by altering that environment. In Scheme:
(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"
Note: Some speakers call any data structure that binds a lexical environment a closure, but the term usually refers specifically to functions.
Implementation and theory
[edit]Closures are typically implemented with a special data structure that contains a pointer to the function code, plus a representation of the function's lexical environment (i.e., the set of available variables) at the time when the closure was created. The referencing environment binds the non-local names to the corresponding variables in the lexical environment at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure. When the closure is entered at a later time, possibly with a different lexical environment, the function is executed with its non-local variables referring to the ones captured by the closure, not the current environment.
A language implementation cannot easily support full closures if its run-time memory model allocates all automatic variables on a linear stack. In such languages, a function's automatic local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore, those variables must be allocated so that they persist until no longer needed, typically via heap allocation, rather than on the stack, and their lifetime must be managed so they survive until all closures referencing them are no longer in use.
This explains why, typically, languages that natively support closures also use garbage collection. The alternatives are manual memory management of non-local variables (explicitly allocating on the heap and freeing when done), or, if using stack allocation, for the language to accept that certain use cases will lead to undefined behaviour, due to dangling pointers to freed automatic variables, as in lambda expressions in C++11[10] or nested functions in GNU C.[11] The funarg problem (or "functional argument" problem) describes the difficulty of implementing functions as first class objects in a stack-based programming language such as C or C++. Similarly in D version 1, it is assumed that the programmer knows what to do with delegates and automatic local variables, as their references will be invalid after return from its definition scope (automatic local variables are on the stack) – this still permits many useful functional patterns, but for complex cases needs explicit heap allocation for variables. D version 2 solved this by detecting which variables must be stored on the heap, and performs automatic allocation. Because D uses garbage collection, in both versions, there is no need to track usage of variables as they are passed.
In strict functional languages with immutable data (e.g. Erlang), it is very easy to implement automatic memory management (garbage collection), as there are no possible cycles in variables' references. For example, in Erlang, all arguments and variables are allocated on the heap, but references to them are additionally stored on the stack. After a function returns, references are still valid. Heap cleaning is done by incremental garbage collector.
In ML, local variables are lexically scoped, and hence define a stack-like model, but since they are bound to values and not to objects, an implementation is free to copy these values into the closure's data structure in a way that is invisible to the programmer.
Scheme, which has an ALGOL-like lexical scope system with dynamic variables and garbage collection, lacks a stack programming model and does not suffer from the limitations of stack-based languages. Closures are expressed naturally in Scheme. The lambda form encloses the code, and the free variables of its environment persist within the program as long as they can possibly be accessed, and so they can be used as freely as any other Scheme expression.[citation needed]
Closures are closely related to Actors in the Actor model of concurrent computation where the values in the function's lexical environment are called acquaintances. An important issue for closures in concurrent programming languages is whether the variables in a closure can be updated and, if so, how these updates can be synchronized. Actors provide one solution.[12]
Closures are closely related to function objects; the transformation from the former to the latter is known as defunctionalization or lambda lifting; see also closure conversion.[citation needed]
Differences in semantics
[edit]Lexical environment
[edit]As different languages do not always have a common definition of the lexical environment, their definitions of closure may vary also. The commonly held minimalist definition of the lexical environment defines it as a set of all bindings of variables in the scope, and that is also what closures in any language have to capture. However the meaning of a variable binding also differs. In imperative languages, variables bind to relative locations in memory that can store values. Although the relative location of a binding does not change at runtime, the value in the bound location can. In such languages, since closure captures the binding, any operation on the variable, whether done from the closure or not, are performed on the same relative memory location. This is often called capturing the variable "by reference". Here is an example illustrating the concept in ECMAScript, which is one such language:
// Javascript
var f, g;
function foo() {
var x;
f = function() { return ++x; };
g = function() { return --x; };
x = 1;
alert('inside foo, call to f(): ' + f());
}
foo(); // 2
alert('call to g(): ' + g()); // 1 (--x)
alert('call to g(): ' + g()); // 0 (--x)
alert('call to f(): ' + f()); // 1 (++x)
alert('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
[edit]var module = {
x: 42,
getX: function() {return this.x; }
}
var unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// emits undefined as 'x' is not specified in global scope.
var boundGetX = unboundGetX.bind(module); // specify object module as the closure
console.log(boundGetX()); // emits 42
Example 2: Accidental reference to a bound variable
[edit]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.[14]
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() {
alert(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.
Closure leaving
[edit]Yet more differences manifest themselves in the behavior of other lexically scoped constructs, such as return
, break
and continue
statements. Such constructs can, in general, be considered in terms of invoking an escape continuation established by an enclosing control statement (in case of break
and continue
, such interpretation requires looping constructs to be considered in terms of recursive function calls). In some languages, such as ECMAScript, return
refers to the continuation established by the closure lexically innermost with respect to the statement—thus, a return
within a closure transfers control to the code that called it. However, in Smalltalk, the superficially similar operator ^
invokes the escape continuation established for the method invocation, ignoring the escape continuations of any intervening nested closures. The escape continuation of a particular closure can only be invoked in Smalltalk implicitly by reaching the end of the closure's code. These examples in ECMAScript and Smalltalk highlight the difference:
"Smalltalk"
foo
| xs |
xs := #(1 2 3 4).
xs do: [:x | ^x].
^0
bar
Transcript show: (self foo printString) "prints 1"
// ECMAScript
function foo() {
var xs = [1, 2, 3, 4];
xs.forEach(function (x) { return x; });
return 0;
}
alert(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"
Closure-like constructs
[edit]Some languages have features which simulate the behavior of closures. In languages such as C++, C#, D, Java, Objective-C, and Visual Basic (.NET) (VB.NET), these features are the result of the language's object-oriented paradigm.
Callbacks (C)
[edit]Some C libraries support callbacks. This is sometimes implemented by providing two values when registering the callback with the library: a function pointer and a separate void*
pointer to arbitrary data of the user's choice. When the library executes the callback function, it passes along the data pointer. This enables the callback to maintain state and to refer to information captured at the time it was registered with the library. The idiom is similar to closures in functionality, but not in syntax. The void*
pointer is not type safe so this C idiom differs from type-safe closures in C#, Haskell or ML.
Callbacks are used extensively in graphical user interface (GUI) widget toolkits to implement event-driven programming by associating general functions of graphical widgets (menus, buttons, check boxes, sliders, spinners, etc.) with application-specific functions implementing the specific desired behavior for the application.
Nested function and function pointer (C)
[edit]With a GNU Compiler Collection (GCC) extension, a nested function[15] 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 <stdio.h>
typedef int (*fn_int_to_int)(int); // type of function int->int
fn_int_to_int 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) {
fn_int_to_int add10 = adder(10);
printf("%d\n", add10(1));
return 0;
}
But moving adder
(and, optionally, the typedef
) in main
makes it valid:
#include <stdio.h>
int main(void) {
typedef int (*fn_int_to_int)(int); // type of function int->int
fn_int_to_int adder(int number) {
int add (int value) { return value + number; }
return add;
}
fn_int_to_int add10 = adder(10);
printf("%d\n", add10(1));
return 0;
}
If executed this now prints 11
as expected.
Local classes and lambda functions (Java)
[edit]Java enables classes to be defined inside methods. These are called local classes. When such classes are not named, they are known as anonymous classes (or anonymous inner classes). A local class (either named or anonymous) may refer to names in lexically enclosing classes, or read-only variables (marked as final
) in the lexically enclosing method.
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.
new Thread(
new Runnable() {
void run() {
// It can read final local variables:
calculate(uri);
// It can access private fields of the enclosing class:
result = result + 10;
}
}
).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,[16] the closure causes the above code to be executed as:
class CalculationWindow extends JFrame {
private volatile int result;
// ...
public void calculateInSeparateThread(final URI uri) {
// The code () -> { /* code */ } is a closure.
new Thread(() -> {
calculate(uri);
result = result + 10;
}).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.[17] They are normally referred to just as "inner classes".[18] 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.[19]
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()) < 10;
/* increment step omitted */) {
System.out.println(i);
}
}
}
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 Function<T,U>
with T being the domain and U the image type. The expression can be called with its .apply(T t)
method, but not with a standard method call.
public static void main(String[] args) {
Function<String, Integer> length = s -> s.length();
System.out.println( length.apply("Hello, world!") ); // Will print 13.
}
Blocks (C, C++, Objective-C 2.0)
[edit]Apple introduced blocks, a form of closure, as a nonstandard extension into C, C++, Objective-C 2.0 and in Mac OS X 10.6 "Snow Leopard" and iOS 4.0. Apple made their implementation available for the GCC and clang compilers.
Pointers to block and block literals are marked with ^
. Normal local variables are captured by value when the block is created, and are read-only inside the block. Variables to be captured by reference are marked with __block
. Blocks that need to persist outside of the scope they are created in may need to be copied.[20][21]
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());
Delegates (C#, VB.NET, D)
[edit]C# anonymous methods and lambda expressions support closure:
var data = new[] {1, 2, 3, 4};
var multiplier = 2;
var 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.
Function objects (C++)
[edit]C++ enables defining function objects by overloading operator()
. These objects behave somewhat like functions in a functional programming language. They may be created at runtime and may contain state, but they do not implicitly capture local variables as closures do. As of the 2011 revision, the C++ language also supports closures, which are a type of function object constructed automatically from a special language construct called lambda-expression. A C++ closure may capture its context either by storing copies of the accessed variables as members of the closure object or by reference. In the latter case, if the closure object escapes the scope of a referenced object, invoking its operator()
causes undefined behavior since C++ closures do not extend the lifetime of their context.
void foo(string myname) {
int y;
vector<string> n;
// ...
auto i = std::find_if(n.begin(), n.end(),
// this is the lambda expression:
[&](const string& s) { return s != myname && s.size() > y; }
);
// 'i' is now either 'n.end()' or points to the first string in 'n'
// which is not equal to 'myname' and whose length is greater than 'y'
}
Inline agents (Eiffel)
[edit]Eiffel includes inline agents defining closures. An inline agent is an object representing a routine, defined by giving the code of the routine in-line. For example, in
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.
C++Builder __closure reserved word
[edit]Embarcadero C++Builder provides the reserved word __closure
to provide a pointer to a method with a similar syntax to a function pointer.[22]
Standard C allows writing a typedef 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)();
See also
[edit]Notes
[edit]- ^ The function may be stored as a reference to a function, such as a function pointer.
- ^ These names usually refer to values, mutable variables, or functions, but can also be other entities such as constants, types, classes, or labels.
References
[edit]- ^ Sussman and Steele. "Scheme: An interpreter for extended lambda calculus". "... a data structure containing a lambda expression, and an environment to be used when that lambda expression is applied to arguments." (Wikisource)
- ^ Turner, David A. (2012). "Some History of Functional Programming Languages" (PDF). International Symposium on Trends in Functional Programming. Lecture Notes in Computer Science. Vol. 7829. Springer. pp. 1–20 See 12 §2, note 8 for the claim about M-expressions. doi:10.1007/978-3-642-40447-4_1. ISBN 978-3-642-40447-4.
- ^ Landin, P.J. (January 1964). "The mechanical evaluation of expressions" (PDF). The Computer Journal. 6 (4): 308–320. doi:10.1093/comjnl/6.4.308.
- ^
Moses, Joel (June 1970). "The Function of FUNCTION in LISP, or Why the FUNARG Problem Should Be Called the Environment Problem". ACM SIGSAM Bulletin (15): 13–27. doi:10.1145/1093410.1093411. hdl:1721.1/5854. S2CID 17514262. AI Memo 199.
A useful metaphor for the difference between FUNCTION and QUOTE in LISP is to think of QUOTE as a porous or an open covering of the function since free variables escape to the current environment. FUNCTION acts as a closed or nonporous covering (hence the term "closure" used by Landin). Thus we talk of "open" Lambda expressions (functions in LISP are usually Lambda expressions) and "closed" Lambda expressions. [...] My interest in the environment problem began while Landin, who had a deep understanding of the problem, visited MIT during 1966–67. I then realized the correspondence between the FUNARG lists which are the results of the evaluation of "closed" Lambda expressions in LISP and ISWIM's Lambda Closures.
- ^ Wikström, Åke (1987). Functional Programming using Standard ML. Prentice Hall. ISBN 0-13-331968-7.
The reason it is called a "closure" is that an expression containing free variables is called an "open" expression, and by associating to it the bindings of its free variables, you close it.
- ^ Sussman, Gerald Jay; Steele, Guy L. Jr. (December 1975). Scheme: An Interpreter for the Extended Lambda Calculus (Report). AI Memo 349.
- ^ Abelson, Harold; Sussman, Gerald Jay; Sussman, Julie (1996). Structure and Interpretation of Computer Programs. MIT Press. pp. 98–99. ISBN 0-262-51087-1.
- ^ "array.filter". Mozilla Developer Center. 10 January 2010. Retrieved 9 February 2010.
- ^ "Re: FP, OO and relations. Does anyone trump the others?". 29 December 1999. Archived from the original on 26 December 2008. Retrieved 23 December 2008.
- ^ Lambda Expressions and Closures C++ Standards Committee. 29 February 2008.
- ^ "6.4 Nested Functions". GCC Manual.
If you try to call the nested function through its address after the containing function exits, all hell breaks loose. If you try to call it after a containing scope level exits, and if it refers to some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk. If, however, the nested function does not refer to anything that has gone out of scope, you should be safe.
- ^ Foundations of Actor Semantics Will Clinger. MIT Mathematics Doctoral Dissertation. June 1981.
- ^ "Function.prototype.bind()". MDN Web Docs. Retrieved 20 November 2018.
- ^ "Closures". MDN Web Docs. Retrieved 20 November 2018.
- ^ "Nested functions".
- ^ "Lambda Expressions". The Java Tutorials.
- ^ "Nested, Inner, Member, and Top-Level Classes". Joseph D. Darcy's Oracle Weblog. July 2007. Archived from the original on 31 August 2016.
- ^ "Inner Class Example". The Java Tutorials: Learning the Java Language: Classes and Objects.
- ^ "Nested Classes". The Java Tutorials: Learning the Java Language: Classes and Objects.
- ^ "Blocks Programming Topics". Apple Inc. 8 March 2011. Retrieved 8 March 2011.
- ^ Bengtsson, Joachim (7 July 2010). "Programming with C Blocks on Apple Devices". Archived from the original on 25 October 2010. Retrieved 18 September 2010.
- ^ Full documentation can be found at http://docwiki.embarcadero.com/RADStudio/Rio/en/Closure
External links
[edit]- Original "Lambda Papers": A classic series of papers by Guy L. Steele Jr. and Gerald Jay Sussman discussing, among other things, the versatility of closures in the context of Scheme (where they appear as lambda expressions).
- Gafter, Neal (28 January 2007). "A Definition of Closures".
- Bracha, Gilad; Gafter, Neal; Gosling, James; von der Ahé, Peter. "Closures for the Java Programming Language (v0.5)".
- Closures: An article about closures in dynamically typed imperative languages, by Martin Fowler.
- Collection closure methods: An example of a technical domain where using closures is convenient, by Martin Fowler.