JavaPulse

a finger on the pulse of the freelance Java™ market in the Netherlands

JavaPolis 2006: Closures

Posted on | 17 December 2006 |
Tags: | |

Closure. Closure. Closure. That’s all I heard from Ruby enthusiasts all week.

It was first explained to me as being able to pass code around, like they had in C which I only vaguely remember. Then it became more clear when applied as the much neglected Visitor Pattern. For example, if you have a Collection and you want to do something to each item. It is really not the caller’s responsibility to loop through the collection. You just want to give the handler method to the collection and say, here you go: run this on each item. That is closure. That is a correct separation of concerns. And more OO.

Another way to use closures is where we currently use anonymous classes. Passing handler code to a listener. Still have to figure out the exact advantage here to anonymous classes.

The use of closures is related to AOP. Closure is passing in handler code, where the use of this code is wrapped by other code, possibly of other aspects, such as starting and ending transactions. It is like passing in the code for the implementation an abstract method, instead of having to create an entire class around it.

So, although the enhanced for-loop in Java 1.5 is a welcomed simplification to the horrid Iterator, it is only moving in the direction of closures, which they say will be in Java 1.6. Until then, the discussion on why and how will be very interesting indeed.

Comments

2 Responses to “JavaPolis 2006: Closures”

  1. RemVee
    December 18th, 2006 @ 10:07

    See http://www.javac.info/ for the proposal discussed at JavaPolis and http://en.wikipedia.org/wiki/Closure_(computer_science) for a explanation about what they are.

  2. Stephan
    December 19th, 2006 @ 09:53

    Hi,

    If interested, I’ve just published the Closures for Java talk by Neal Gafter on Parleys.com.

    Enjoy,
    Stephan

Leave a Reply