Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
Freenet DSL
Who's Online
11 user(s) are online (10 user(s) are browsing encyclopedia)

Members: 0
Guests: 11

more...
browser tip
recommendation!
Sponsored
partner

Closure (computer science)

In programming languages, a closure is an abstraction representing a function (programming), plus the lexical environment (see static scoping) in which the function was created.

=Implementation and Theory=

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 and their values) at the time when the function was created.

Closures are closely related to Actors in the Actor model of concurrent computation where the values 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 a solution. (See the doctoral dissertation of Will Clinger.)

=Closures and functions=

Closures typically appear in languages that allow functions to be first-class object—in other words, such languages allow 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, in ML programming language, the following code defines a function f that returns its argument addition 1: fun f(x) = x + 1; Such a function may capture name/value bindings from its enclosing environment, producing a closure. For example, in the code fragment: val x = 1; fun f(y) = x + y; the closure data structure representing f contains a pointer to the enclosing environment, in which x is bound to 1. Therefore, f will always return its argument plus 1, even if the environment in which it is applied has a different value for x. Therefore, consider the code fragment: let val x = 1; fun f(y) = x + y; in let val x = 2; in f(3) end end In this code, the call f(3) occurs in an environment (the inner let) where x is bound to 2. However, the closure for f was constructed in an environment (the outer let) where x is bound to 1. Therefore the result of the call f(3) is 4, not 5.

=Uses of Closures=

Closures have many uses:

  • Designers of library (computer science) can allow users to customize behavior by passing closures as arguments to important functions. For example, a function that sort algorithms values can accept a closure argument that compares the values to be sorted according to a user-defined criterion.
  • 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 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 as well.
  • Multiple functions can be produced which close over the same environment, enabling them to communicate privately by altering that environment.
  • Note: Some speakers call any data structure that binds a lexical environment a closure, but the term usually refers specifically to functions.

    =Programming languages with closures=

    Scheme programming language was the first programming language to have fully general, lexically scoped closures. Virtually all functional programming languages, as well as the Smalltalk-descended object-oriented programming languages, support some form of closures. Some prominent languages that support closures include:

  • Haskell programming language
  • Lisp programming language (including Scheme programming language)
  • JavaScript
  • ActionScript
  • All variants of ML programming language (including Ocaml and Standard ML)
  • Python programming language
  • Perl
  • Ruby programming language
  • Sleep programming language
  • Smalltalk
  • Oz programming language
  • Lua programming language
  • Boo programming language
  • Groovy
  • Csharp from version 2.0
  • Nemerle
  • Eiffel programming language
  • =Simulating closures=

    Some object-oriented languages enable the programmer to use objects to simulate some features of closures. For example:

  • Java programming language allows the programmer to define anonymous class (object-oriented programming)es inside a method (object-oriented programming); an anonymous class may refer to names in lexically enclosing classes, or final names in the lexically enclosing method.
  • public interface Function { To apply(From aArgument); } ... public final Function createAdder(final int x) { return new Function() { Integer apply(Integer aArgument) { return x+aArgument.intValue(); } } } ...

    (Note that this example uses Java 1.5/5.0 s generics, autoboxing and autounboxing syntax.)

  • In C plus plus and D programming language, programmers may define classes that overload the () (function application) operator. Instances of such classes are called function objects, or occasionally functors (although the latter term is confusing, because it has a very different meaning in other programming languages). Such function objects behave somewhat like functions in a functional programming language, but they are different from closures in that variables from their environment are not captured. To approximate an actual closure, one can imagine placing all global variables in a single struct, a copy of which can be passed to a function object.
  • C Sharp programming language allows a delegate to store reference to method of class instance; when calling such delegate, the method is invoked for the particular instance. To approximate an actual closure, one can create class instance (i.e. object) with required data members. Another way to support closures was introduced in version 2.0 of C# using anonymous methods .
  • =See also=

  • funarg problem
  • spaghetti stack
  • Value-level programming
  • =Reference=

    *Will Clinger. Foundations of Actor Semantics MIT Mathematics Doctoral Dissertation. June 1981.

    =External links=

    *[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 expressions ). *[http://c2.com/cgi/wikiWhatIsClosure Description from the Portland Pattern Repository] *[http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlfaq7/What s_a_closure.html What s a closure (Perl FAQ)] *[http://wiki.cs.uiuc.edu/VisualWorks/Closures Description from the VisualWorks wiki] *[http://boost.org/libs/spirit/doc/closures.html Closures by the Spirit library of Boost] *[http://developer.gnome.org/doc/API/2.0/gobject/gobject-Closures.html Description from the GObject Reference Manual] *[http://www.jaxlib.org/docs/api/jaxlib/closure/class-use/Function.html Uses of Interface jaxlib.closure.Function] *[http://jibbering.com/faq/faq_notes/closures.html Javascript Closures] *[http://ivan.truemesh.com/archives/000392.html Python closures] *[http://steike.com/PhpClosures PHP Closures] *[http://sleep.hick.org/sleeplang.html#10a Sleep Closures] *[http://doi.acm.org/10.1145/62678.62687 Syntactic Closures (A 1988 paper, by Alan Bawden and Jonathan Rees, that discusses a special use of closures in LISP syntax macros)] *[http://www.martinfowler.com/bliki/Closure.html Martin Fowler s Bliki: Closure]