Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Catching up with Closures for Java

Catching up with Closures for Java

This item in japanese

Neal Gafter recently gave a presentation at JavaOne and Jazoon '07 entitled Closures for Java: ... and other thoughts on language evolution (presentation, mp3 audio, simultaneous presentation/audio).

The presentation is a gentle, but relatively thorough introduction to closures, the goals, the problem with existing solutions, all presented in an accessible, conversational style.  If you've avoided reading some of the deeply technical proposals in the past, this is a good way to get an introduction and overview to the closures proposal.

He opens the presentation with this quotation from Mark Jason Dominus from The Perl Review:

In another thirty years people will laugh at anyone who tries to invent a language without closures, just as they’ll laugh now at anyone who tries to invent a language without recursion.

Neal Gafter described the goals of closures as enabling developers to accomplish the same kinds of things that they currently do with anonymous inner classes, but with less pain; integrate with existing apis, enable control APIs, functional and aggregate operations, and to be simple, but powerful.

By enabling control APIs, Gafter hopes to reduce boilerplate code, where documentation describes how to use an API with marked sections where you might add your own code, and that developers often cut-and-paste, simply because Java does not have a clear and concise way of expressing this API form.

Neal Gafter walked through how one might implement the Java SE 5 for-each loop using anonymous inner classes, pointing out the flaws in the approach along the way:

All of these examples show places where you know how to solve the problem you're trying to solve. You already have the solution in mind; it's really clear. The difficulty becomes expressing it in the Java programming language and there you have to fight with the language and fight with the compiler to let you do what you know how to do.

He then describes the syntax and semantics of the Closures proposal, and uses these in examples, from control apis with locks, through aggregate operations to Swing event listeners. He also described the new APIs that could result from language-support for Closures.

The presentation was well-received by attendees, who tended to believe that Closures for Java are important or inevitable, but others remain unconvinced. Fabrizio Gianneschi wanted to see this presentation "because I'm against closures, I missed his previous talks, I'm following his thoughts on the Java Champion's mailing lists and I would like to get convinced that closures are useful".

For more information, visit the Closures for the Java programming language website, or continue reading about closures at InfoQ.

Rate this Article