Closure (computer programming): Difference between revisions

Content deleted Content added
m Nested function and function pointer (C): "provided" is the more typical grammatical use in writing per m-w.com and https://dictionary.cambridge.org/grammar/british-grammar/conditionals-other-expressions-unless-should-as-long-as
Small WP:COPYEDITs WP:EoS: WP:TERSE, clarify. WP:LINK update-standardize. Cut needless carriage returns in sentences, paragraphs, section. WP:CATEGORYs reorder-alphabetize.
Line 3:
{{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 [[valueValue (computer science)|value]] or [[Reference (computer science)|reference]] to which the name was bound when the closure was created.{{efn|These names most frequentlyusually 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 ==
Line 74:
 
== 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]] languages such as [[Lisp (programming language)|Lisp]] and [[ML (programming language)|ML]], as well asand 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]].
[[Julia (programming language)|Julia]],
[[Python (programming language)|Python]] and
[[Rust (programming language)|Rust]].
Closures are also frequently used with [[Callback (computer programming)|callback]]s, 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 [[informationInformation hiding|hide state]]. Constructs such as [[objectObject (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 ===
Line 96 ⟶ 92:
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 itself has a reference to <code>threshold</code>, it can use that variable each time <code>filter</code> calls it. The function <code>filter</code> itself might be defined in a completely separate file.
 
Here is the same example rewritten in [[JavaScript]], another popular language with support for closures:
Line 149 ⟶ 145:
 
== 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 itself. 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.
Line 207 ⟶ 203:
 
===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">
Line 238 ⟶ 233:
</syntaxhighlight>
 
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 itself when the closure is invoked, and actuallythen attempts to use the captured binding.
 
=== Closure leaving ===
Line 334 ⟶ 329:
 
=== Callbacks (C) ===
Some [[C (programming language)|C]] libraries support [[Callback (computer programming)|callbacks]]. This is ometimes 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.
Some [[C (programming language)|C]] libraries support
[[callback (computer science)|callback]]s. 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 extensively used in 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.
Callbacks are extensively used in 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)====
Line 422 ⟶ 401:
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.
 
Capturing of variables by reference can be emulated by using a <code>final</code> reference to a mutable container, for example, a singleone-element array. The local 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.
 
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:
 
<syntaxhighlight lang="java">
Line 592 ⟶ 571:
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 itself 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 ===
Line 643 ⟶ 622:
[[Category:Implementation of functional programming languages]]
[[Category:Subroutines]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Scheme (programming language) code]]
[[Category:Articles with example JavaScript code]]
[[Category:Articles with example C++ code]]
[[Category:Articles with example Eiffel code]]
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example D code]]
[[Category:Articles with example Objective-CEiffel code]]
[[Category:Articles with example Scheme (programming language)Haskell code]]
[[Category:Articles with example Java code]]
[[Category:Articles with example JavaScript code]]
[[Category:Articles with example EiffelObjective-C code]]
[[Category:Articles with example Python (programming language)|Python code]] and
[[Category:Articles with example Ruby code]]
[[Category:Articles with example PythonScheme (programming language) code]]
[[Category:Articles with example Smalltalk code]]
[[Category:Articles with example Haskell code]]