Closure (computer programming) - Wikipedia
closuresfunctional-programmingprogramming-languageslexical-scopinglambda-calculus
Abstraction: Lexical closures — functions bundled with their captured environment
Key points:
- A closure is a record pairing a function with its lexical environment, capturing free variables at creation time so they remain accessible outside their original scope.
- The term was defined by Peter Landin in 1964 (SECD machine); first fully implemented in 1970 in the PAL language; popularized by Sussman and Steele in Scheme (1975).
- Closures require variables to outlive the enclosing scope — this necessitates heap allocation and typically garbage collection; stack-based languages (C, C++) must handle this specially.
- Capture semantics vary: by reference (ECMAScript, C++
[&]) vs. by value (ML, Javafinal, C++[=]); lazy languages like Haskell capture computations. - Common uses include event callbacks, partial application, state encapsulation (analogous to private fields), and implementing control structures (Smalltalk uses closures for all if/while/for).
- Languages differ on how
returninteracts with closures: ECMAScriptreturnexits the closure only; Smalltalk^exits the enclosing method.
Connections: Peter Landin · Scheme · Closures · Functional Programming · Lexical Scoping · First Class Functions
Source: http://en.wikipedia.org/wiki/Closure_%28computer_science%29