CSC/ECE 517 Fall 2010/ch1 n 00
Closures in Ruby vs. closures in other languages - Introduction to Closure
What is Closure?
There is no single definition for Closure. The definition of Closure is closely associated with the language that implements Closure. Wikipedia defines Closure as follows:
A Closure is a first-class function with free variables that are bound in the lexical environment.
One of the earliest known programming language that implemented Closure, Lisp defines Closure as follows:
A closure is a function that captures the bindings of free variables in its lexical context.
SmallTalk an object oriented language defines closure differently as follows:
A closure is a function that binds all the free variables appearing in methods to the scope of the object that the method is a member of.
A not so valid, but an easy to understand definition for Closure is given as:
Closure is a special function pointer which has access to the local variables of the enclosing function, where the Closure is created.
More convincing, easy to understand definition is from 5:
- It can be passed around as a value and
- executed on demand by anyone who has that value, at which time
- it can refer to variables from the context in which it was created (i.e. it is closed with respect to variable access, in the mathematical sense of the word "closed").
Are you lost? But don’t worry, there is a lot of debate over what closure should be, how it should work and how it should be implemented? You will find lot of discussions and debates about this topic in the Internet. For more details and historic development of Closure is available here 1