CSC/ECE 517 Fall 2010/ch1 n 00

From Expertiza_Wiki
Revision as of 19:11, 4 September 2010 by Cgoogc (talk | contribs)
Jump to navigation Jump to search

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

What Closure is not

This section attempts to clarify some of the general misconceptions about Closure. Those who have never experienced Closure, might relate things close to Closure as Closure.

  • C Programmer: Might tend to think Closures are like Function Pointers. But that is not true, Closure is more than function pointers. [why??] Function pointers are not considered First-Class Objects.
  • Java Programmer: Inner Classes are similar to Closures but not Closures exactly. Inner class can access only instance variables of the surrounding class and has only READ-ONLY access to the local variables. The local variables must be marked final to make them available for the inner class to use them.

Why Closure is needed?

If you think, Closure is not a mandatory requirement for a language. Many popular programming languages like C, Java, C++ does not have Closure. Closure adds more convenience to a language, at the same time it makes it difficult for first time users. With Closure, you can really write short code and avoid passing too many arguments, since Closure closes the local variables.

When to use Closure?

Closure is a very handy tool which makes the code less verbose. You can use Closure where you used Function Pointers, Anonymous Functions(), Anonymous Inner Classes. This is a very common piece of code in Java for creating a Thread.

Thread t = new Thread(new Runnable() { public void run() { // thread logic } });

The above piece of code if re-written with Closures take a very simple form as follows:

Thread t = new Thread(#() { // thread logic });

Above statement is written using one of proposed Closure syntax addition for Java 7 (Java does not have Closures yet). You can see that the code is lot less verbose and neat. When Closures are available in Java, you can see that you can do away with most of the Callback classes used in Java.

Closures in the form of Delegates are very actively used in C#. Delegates are used to add and remove event listener in C# elegantly. To read more about the use of Delegates in C# see 3.

External links