InfoQ

News

Catching up with Closures for Java

Posted by Geoffrey Wiseman on Aug 29, 2007 07:45 AM

Community
Java
Topics
Tags
JavaOne 2007,
Closures,
Java SE

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.

14 comments

Reply

Closures = Syntactic Sugar by Dmitriy Setrakyan Posted Aug 29, 2007 10:40 AM
Re: Closures = Syntactic Sugar by Tom Nichols Posted Aug 29, 2007 11:04 AM
Re: Closures = Syntactic Sugar by Stefan Arentz Posted Aug 29, 2007 11:06 AM
Re: Closures = Syntactic Sugar by Dmitriy Setrakyan Posted Aug 29, 2007 11:39 AM
Re: Closures = Syntactic Sugar by Ricky Clarkson Posted Aug 29, 2007 12:35 PM
Re: Closures = Syntactic Sugar by Dmitriy Setrakyan Posted Aug 29, 2007 12:59 PM
Re: Closures = Syntactic Sugar by Diego Fernandez Posted Aug 29, 2007 3:13 PM
Re: Closures = Syntactic Sugar by Daniel Serodio Posted Aug 30, 2007 11:00 AM
Re: Closures = Syntactic Sugar by Ricky Clarkson Posted Aug 30, 2007 7:48 AM
Re: Closures = Syntactic Sugar by Jonathan Allen Posted Aug 29, 2007 6:19 PM
Re: Closures = Syntactic Sugar by Robert Dean Posted Aug 29, 2007 6:00 PM
Excellent presentation by Mike Heath Posted Aug 29, 2007 11:33 AM
Re: Excellent presentation by Geoffrey Wiseman Posted Aug 29, 2007 6:25 PM
Re: Excellent presentation by Joe Bedard Posted Aug 30, 2007 4:28 PM
  1. Back to top

    Closures = Syntactic Sugar

    Aug 29, 2007 10:40 AM by Dmitriy Setrakyan

    I am still kind of skeptical about closures. From what I have seen so far, it does not look like an brand new concept (unlike generics or annotation in Java 5). Instead I would qualify them as syntactic sugar for anonymous Java classes, much like 'for each' loop which removed indexes from Java 'for' loops. However, while the advantage of 'for each' was always evident to me due to its simplicity, I am not sure the same applies here. Beast, Dmitriy GridGain - Grid Computing Made Simple

  2. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 11:04 AM by Tom Nichols

    Sugar or not, I would take it in a heartbeat any day. To be able to pass around a piece of code that has a well-defined way to "execute" it is gold. Think about set operations on collections, i.e. Groovy's collect method. Something that is incredibly common, but we just tend to avoid b/c it's not convenient to write in Java. But think about doing that stuff using anonymous classes all the time. You'd want to shoot yourself.

  3. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 11:06 AM by Stefan Arentz

    I am still kind of skeptical about closures. From what I have seen so far, it does not look like an brand new concept (unlike generics or annotation in Java 5).
    Huh. Nothing new? How can you say that? Look at the slides and the time("foo") { } example .. nothing new? You must be joking. This closure proposal lets you not just define anonymous method expressions, it lets you easily define new language constructs. Something that is very powerful and very much needed in Java imo.
    Instead I would qualify them as syntactic sugar for anonymous Java classes,
    This is a pretty lame thing to say. With that sort of reasoning you might as well reject all new java language proposals because each of them is syntactic sugar for things you could already do with plain java code. That is completely besides the point here. Closures will make the language more powerful and will allow you to do powerful things in an easy way. Wouldn't it be great to get rid of the TransactionTemplate stuff in Spring and be able to simply say: withinTransaction(tx) { updateDb(); } instead of ugly many line solution that we have now?
    much like 'for each' loop which removed indexes from Java 'for' loops.
    I don't think you understand how foreach works. It has nothing to do with removing indexes. It has everything to do with adding a new concept with a specific contract (Iterable) to the language. Indexes are not part of that contract. S.

  4. Back to top

    Excellent presentation

    Aug 29, 2007 11:33 AM by Mike Heath

    This is an excellent presentation. Most of the closure nay-sayers I know personally have changed their tune about closures after watching this presentation. To be frank, suggesting that closures are just syntactic sugar is ignorant of what closures have to offer. Closures add functionality and flexibility to the language that can't be done with inner classes. Granted, a lot of what closures have to offer is syntactic sugar but it's syntactic sugar that greatly increases code readability and reduces the need for boilerplate code. IMO, this is a must for the Java language. Please, watch the presentation. I promise it's worth your time.

  5. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 11:39 AM by Dmitriy Setrakyan

    Instead I would qualify them as syntactic sugar for anonymous Java classes,
    This is a pretty lame thing to say. With that sort of reasoning you might as well reject all new java language proposals because each of them is syntactic sugar for things you could already do with plain java code. That is completely besides the point here.
    Disagree. Many new ideas that came in Java 5 were true new features, like annotations and generics - they simply were not possible in Java 4. Closures, on the other hand, is an enhancement, not a brand new feature. Again, I have nothing against enhancements, but the way they are being introduced, in my opinion, defeats regular Java semantics. For example, how do you do exception handling with closures? I think the current proposal forces you to throw runtime exceptions. What if I want to have a checked exception - am I out of luck? Best, Dmitriy GridGain - Grid Computing Made Simple

  6. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 12:35 PM by Ricky Clarkson

    I don't understand why Dmitriy is skeptical, on the grounds that closures are a trivial feature. Surely the more trivial they are the less disruption they can cause. In fact, Neal has gone to a lot of trouble to make sure that they fit well in the existing language, so if you think they seem trivial, that's to Neal's credit. As to them being syntax sugar - you can call everything syntax sugar, above what is needed to be Turing-complete. As it happens, Java's syntax is in need of some sugar, especially when you want to write functional code. Annotations aren't so much of a new feature, people were already doing them in javadocs using xdoclet, etc. Java 5 annotations are certainly an improvement on that. Generics are the ultimate syntax sugar, right, because of erasure? You're wrong, Dmitriy, about semantics - as far as possible the semantics will be preserved. The only contentious issue I know of is last-line-no-semicolon for return values from closures (which I disagree with, though I know the reasoning). All your keywords are belong to you. Inside a closure, return means the same as outside it, as does this, continue, break and throw. Yes, you're wrong about throwing exceptions - the current proposal does not force you to throw runtime exceptions.

  7. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 12:59 PM by Dmitriy Setrakyan

    For example, how do you do exception handling with closures? I think the current proposal forces you to throw runtime exceptions. What if I want to have a checked exception - am I out of luck?
    Ooops, I was wrong here :-) I missed the support for checked exceptions. It was not apparent from presentation, had to look into JSR specification for that. However once you start putting exception declarations into your boiler-plate code, it does make it less readable. But I can see the point of JSR, cases with multiple exceptions thrown out of the same block are rather rare. As far as syntax sugar, I guess you can stretch my statement and say that everything can be called a syntax sugar. I just wanted to point out, that unlike Annotations or Generics, there is currently support in Java language via anonymous classes for practically everything closures provide. However, I do agree that closures make it much simpler in many cases, but they won't be as easy to grasp for developers as Java5 'for each' syntax though. Best, Dmitriy GridGain - Grid Computing Made Simple

  8. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 3:13 PM by Diego Fernandez

    As far as syntax sugar, I guess you can stretch my statement and say that everything can be called a syntax sugar. I just wanted to point out, that unlike Annotations or Generics, there is currently support in Java language via anonymous classes for practically everything closures provide. However, I do agree that closures make it much simpler in many cases, but they won't be as easy to grasp for developers as Java5 'for each' syntax though.
    You are wrong here, full closure support is not trivial, in fact is very difficult to implement. That's why Java doesn't allow to reference non-final variables from anonymous inner classes. To implement full closure support the VM has to be very intelligent on the handling of activation records (take a look at Closures in VisualWorks). In Smalltalk for example closures are used to implement if/else using objects and messages:
    | result |
    (a > 10) ifTrue: [ result := a ] ifFalse: [ result := b ].
    In this code fragment result is modified inside a closure (this is impossible to do with Java anonymous inner classes). Note that #ifTrue:ifFalse: is not a language keyword, just a method declared in the Boolean class. In St [] is used for closures, and
    booleanObject ifTrue: block1 ifFalse: block2
    is like
    booleanObject.ifTrueIfFalse(block1, block2)
    in Java. I had worked with Smalltalk during 3 years, and the top of my list of missing features in Java are: the hability to evaluate code everywhere, no static type checking and closures.

  9. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 6:00 PM by Robert Dean

    I think closures bring the same kind of simplicity that the for-each syntax does. Instead of having to construct an inner class with an overridden method, it'd be much better to just pass around a closure.

  10. Back to top

    Re: Closures = Syntactic Sugar

    Aug 29, 2007 6:19 PM by Jonathan Allen

    Disagree. Many new ideas that came in Java 5 were true new features, like annotations and generics - they simply were not possible in Java 4.
    Generics are just syntatic sugar in Java as well. At runtime there is no way to tell the difference between a Box and a Box, both are compiled as Box.

  11. Back to top

    Re: Excellent presentation

    Aug 29, 2007 6:25 PM by Geoffrey Wiseman

    This is an excellent presentation. Most of the closure nay-sayers I know personally have changed their tune about closures after watching this presentation. ... Please, watch the presentation. I promise it's worth your time.
    Agreed; I was in favor of closures anyway, having struggled with control-api issues in most projects, but I hadn't really sat down to enumerate the reasons why anonymous inner classes weren't just slightly uglier syntax. Going through the examples one by one really made an impact on me.

  12. Back to top

    Re: Closures = Syntactic Sugar

    Aug 30, 2007 7:48 AM by Ricky Clarkson

    However once you start putting exception declarations into your boiler-plate code, it does make it less readable.
    What does that have to do with closures?
    As far as syntax sugar, I guess you can stretch my statement and say that everything can be called a syntax sugar.
    Yes, my aim there was to make it clear that dismissing something because it's syntax sugar is fallacy.
    I just wanted to point out, that unlike Annotations or Generics, there is currently support in Java language via anonymous classes for practically everything closures provide.
    Of course there is. C programmers can emulate closures too. Neal points out pretty good reasons why you want actual closures instead of emulations thereof.
    However, I do agree that closures make it much simpler in many cases, but they won't be as easy to grasp for developers as Java5 'for each' syntax though.
    So they're trivial and too hard at the same time? It will be worthwhile for developers to learn how to use closures, for programming in general, and for Java. Diego Fernandez, To allow non-final variables (other than fields) from the enclosing scope, one needs to place the variable in the closure object and have the enclosing scope read and write to that. I don't think it's incredibly hard - apparently it was disallowed in the first place for (false) performance reasons.

  13. Back to top

    Re: Closures = Syntactic Sugar

    Aug 30, 2007 11:00 AM by Daniel Serodio

    I had worked with Smalltalk during 3 years, and the top of my list of missing features in Java are: the hability to evaluate code everywhere, no static type checking and closures.
    I think Groovy is what you're looking for.

  14. Back to top

    Re: Excellent presentation

    Aug 30, 2007 4:28 PM by Joe Bedard

    I read through the presentation and was not convinced that closure are an improvement to Java. Looking at page 4 and 5: Where is the closure defined at? Somewhere else in the class? Do I have to jump to some other place in the code to read the closure? For me, the example was easier to read and understand before the closure was applied because it was all in one place. Looking at page 6: We don't need closures to simplify the example to: for (Map.Entry e : myMap.entrySet()) { doSomething(e.getKey(), e.getValue()); } Looking at page 7: What about listeners with more than one method like MouseListener? Anonymous classes have at least one advantage over closures because they can be defined where they are needed in the code. My impression is that closures require an additional definition (of the closure) somewhere else in the code--which means they should be compared with the other three kinds of nested classes (static nested, inner, and local. see http://java.sun.com/docs/books/tutorial/java/javaOO/summarynested.html) Unlike closures, anonymous classes are not intended for reuse. Do closures provide any benefit over the other kinds of nested classes?

Exclusive Content

Agile Project Management: Lessons Learned at Google

In this presentation filmed during QCon 2007, Jeff Sutherland, the creator of Scrum, talks about his visit at Google to do an analysis of Google's first implementation of Scrum.

AtomServer – The Power of Publishing for Data Distribution

In this article, Bryon Jacob and Chris Berry introduce AtomServer, their implementation of a full-fledged Atom Store based on Apache Abdera, which is now available as open source.

An Introduction to Virtualization

It is easy to think that virtualization applies only to servers. In reality the recent resurgence of the concept is also being applied to networking, storage, and application infrastructure.

REST Anti-Patterns

In this article, Stefan Tilkov explains some of the most common anti-patterns found in applications that claim to follow a "RESTful" design and suggests ways to avoid them.

Choosing between Routing and Orchestration in an ESB

In this article, Adrien Louis and Marc Dutoo discuss the differences and relative merits of using orchestration vs. routing in a typical ESB setup, and discuss various implementation options.

Enterprise Batch Processing with Spring

Wayne Lund discusses batch processing, Spring Batch objectives and features, scenarios for usage, Spring Batch architecture, scaling, example code, failures and retrying, and the future roadmap.

User Story Estimation Techniques

Developer Jay Fields draws on his experiences as a ThoughtWorks consultant to describe effective user story estimation techniques.

Security (CAS and OpenID) with Ruby

In this talk from QCon SF 2007, Justin Gehtland explains two open solutions to distributed identity and their Rails integration components: OpenID (using ruby-openid) and CAS (using rubycas-client).