Content deleted Content added
Citation bot (talk | contribs) Add: pages. Removed parameters. Some additions/deletions were parameter name changes. | Use this bot. Report bugs. | Suggested by Abductive | #UCB_webform 2470/3850 |
Jerryobject (talk | contribs) WP:REFerence WP:CITation parameters: updates, corrects, respaces, reorders, adds, fills, author > last + first, conform to master templates. Small WP:COPYEDITs WP:EoS: WP:TERSE, clarify, 2nd MOS:PERSON WP:YOUs fix-cut. WP:LINKs: adds, update-standardizes, needless WP:PIPEs > WP:NOPIPEs. MOS:FIRSTABBReviation clarify, define before WP:ABBR in parentheses. Cut needless carriage returns in: WP:CITations, paragraphs. |
||
Line 1:
{{Short description|Technique for creating lexically scoped first class functions}}
{{
{{
{{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 [[
== History and etymology ==
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 [[Rpal|PAL]] programming language to support lexically scoped [[first-class function]]s.<ref name=dat2012>[[David A. Turner]] (2012). [http://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf "Some History of Functional Programming Languages"]. Trends in Functional Programming '12. Section 2, note 8 contains the claim about M-expressions.</ref>
[[Peter
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
== Anonymous functions ==
Line 108:
</syntaxhighlight>
The <code>function</code> keyword is used here instead of <code>lambda</code>, and an <code>Array.filter</code> method<ref>{{cite web |
A function may create a closure and return it, as in
<syntaxhighlight lang="javascript">
Line 148:
</syntaxhighlight>
* Closures can be used to implement [[Object-oriented programming|object]] systems.<ref>{{cite web |
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.
Line 222:
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
Yet another subset, [[lazy evaluation|lazy]] functional languages such as [[
<syntaxhighlight lang="haskell">
Line 241:
=== Closure leaving ===
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.
<syntaxhighlight lang="smalltalk">
Line 355:
====Nested function and function pointer (C)====
With a
<syntaxhighlight lang="c">
Line 419 ⟶ 418:
</syntaxhighlight>
The capturing of <code>final</code> variables enables
Capturing of variables by reference can be emulated by using a <code>final</code> reference to a mutable container, for example, a single-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 (The Java Tutorials)}}</ref> the closure causes the above code to be executed as:
<syntaxhighlight lang="java">
Line 442 ⟶ 438:
</syntaxhighlight>
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>
{{cite web |
|
{{cite web |
|
}}</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 |
|
}}</ref>
Line 499 ⟶ 495:
=== 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 [[
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 |
<syntaxhighlight lang="objc">
Line 599 ⟶ 595:
Embarcadero C++Builder provides the reserve word __closure 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>
typedef void (*TMyFunctionPointer)( void );
</syntaxhighlight>In a similar way
typedef void (__closure *TMyMethodPointer)();
</syntaxhighlight>
Line 626 ⟶ 622:
== 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
|last=Gafter
|
|
|
}}
* {{cite web
|last1=Bracha
|
|
}}
*[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]].
|