InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Closures Proposed for Java SE 7

Posted by Floyd Marinescu on Aug 21, 2006

Sections
Development,
Architecture & Design
Topics
Programming ,
Java
Tags
Java SE ,
Closures ,
Language Features
Gilad Bracha, Neal Gafter, James Gosling, and Peter von der Ahé (some of the main architects of the Java language) on Friday put out a proposal for adding closures and local functions to Java SE 7, a feature that Smalltalk users always raved about, which is common in scripting langauges and even C# supports them. 
For programming in the small, closures allow one to abstract an algorithm over a piece of code; that is, they allow one to more easily extract the common parts of two almost-identical pieces of code. For programming in the large, closures support APIs that express an algorithm abstracted over some computational aspect of the algorithm. We propose to add function types and closures to Java. We anticipate that the additional expressiveness of the language will simplify the use of existing APIs and enable new kinds of APIs that are currently too awkward to express using the best current idiom: interfaces and anonymous classes.
Peter commented further on how anonymous classes aren't good enough to simulate closures.   Gilad Bracha blogged a bit about why the proposal is coming now, saying that "the widespread adoption of scripting languages has exposed a lot of people to the idea of first class functions. It has also shown that another argument used against closures - that they are too abstract for ordinary programmers - is just as imbecilic as the one about customer demand." 

DWR founder Joe Walker also wrote about how the Collections classes would need to be modified to support closures:
void(int) totalizer = (int i) : total = total + i;
totalizer(1);
totalizer(2);
System.out.println(total); // Prints 3
int total=0;

But what you really want is to be able to do things like this:

int[] numbers = new int[] { 100, 200 };
numbers.each(totalizer);
System.out.println(total); // Now prints 303;
Javalobby posted a link to the proposal on Friday which drew a large discussion of developers mostly supportive of the idea.   Neal Gafter on saturday wrote a detailed justification of the benefit of closures,  showing a set of code samples and how closures simplified them significantly. Neal's conclusion: closures "allow you to easily abstract away aspects of code that would otherwise require complex contortions.
Or..."Closures proposed for Java 2015" by Clinton Begin Posted
Re: Or...'Closures proposed for Java 2015' by Floyd Marinescu Posted
Re: Or...'Closures proposed for Java 2015' by j c Posted
Re: Or...'Closures proposed for Java 2015' by Clinton Begin Posted
Re: Or...'Closures proposed for Java 2015' by j c Posted
Gilad Bracha will be happy by Joost de Vries Posted
Is it really needed? by Bahadir Yagan Posted
Re: Is it really needed? by Diego Visentin Posted
PL1/C++ Syndrome by Diego Visentin Posted
  1. Back to top

    Or..."Closures proposed for Java 2015"

    by Clinton Begin

    Yeah, I think Sun should adopt the old Windows version convention of using years, so that nobody is confused about when they'll actually be able to use this.

    If Sun wants to have ANY hope of keeping Java moving as fast as it needs to, here's what they need to do one thing:

    Separate the release cycles of javac, java and rt.jar. In other words, let the Java language, the Java VM and the core class library evolve at different speeds. Obviously there will be dependencies, but with these three things, it's pretty easy to figure out:

    JVM <- Java <- Core Classes

    The nice thing is that most of the features I want in Java won't require a change to the JVM. Furthermore, the JVM changes the least often, then Java and the core class library changes the most -- usually with additions rather than changes.

    I think this would dramatically increase the speed at which some of these features can be realized.

    Closures will not require a change to the VM...so why do we need to wait 6 years before we see it?

    Clinton

  2. Back to top

    Re: Or...'Closures proposed for Java 2015'

    by Floyd Marinescu

    Clinton, that sounds like a fine idea. I suppose there could be questions about compatibility (would you have to version the JVM separately from the language and core classes?), but it could work.

  3. Back to top

    Re: Or...'Closures proposed for Java 2015'

    by j c

    Java has claimed to be best of breed of general purpose for a long time. Rather than me too, is there anything better about Java closures or is this just another game of catch up which not many people will be interested in?

    the article starts...Modern Languages do THIS. Oh you mean except for Java. And we all made excuses for the lack of it for years as if we prayed to SUN to do it and noone did noone wanted it until the SHINE of Java wore off a bit and now its like a direction?

    Sounds like a patch.

  4. Back to top

    Gilad Bracha will be happy

    by Joost de Vries

    Back in 2001 when jsr 14 (generics) was in public review I consciously allowed myself to be idealistic/naive enough to give feedback to this review; I made the familiar argument that generics plus anonymous inner classes almost equals closures so only minimal syntactic sugar would be needed.

    Gilad Bracha was gracious enough to react and say that he did not expect it to happen after arguing 6 yrs for closures.

  5. Back to top

    Is it really needed?

    by Bahadir Yagan

    I am not an expert on the subject but after reading for a while, it looked like C++ Function Objects or C function pointers. I don't see any good in making the language more complex with such constructs.

    Apart from the API we have, it looks like the Java itself will endup C++ with GC.

  6. Back to top

    PL1/C++ Syndrome

    by Diego Visentin

    IMHO, Java and C# are following the PL1 or C++ path, ie add complexity on every language revision without understand that there isn't a good language for everykind of work. Java and C# run over a VM, so you can use other specific languages for specific tasks, like VB.NET or Groovy or Lisp, is supported by desing.
    So, why can we try to add complexity to a 'clear' language?

    PS: with modern multi-language IDE like Eclipse is really simple for a developer to master multiple language artifacts per project like html, javascript, sql, java; add to this list groovy/jruby/jpyhon/jlisp/etc. and leave java (language) small and simple.

    Ciao, Diego

  7. Back to top

    Re: Or...'Closures proposed for Java 2015'

    by Clinton Begin

    >>Modern Languages do THIS. Oh you mean except for Java.

    Hmmm...modern languages...

    Like the 12 year old Ruby language?


    Java was out of date the day it came out. Back then it didn't have to try hard, because it was competing against crap (VB and Perl). Sun failed to compete when they needed to.

    Cheers,
    Clinton

  8. Back to top

    Re: Or...'Closures proposed for Java 2015'

    by j c

    >>Modern Languages do THIS. Oh you mean except for Java.

    Hmmm...modern languages...

    Like the 12 year old Ruby language?


    Java was out of date the day it came out. Back then it didn't have to try hard, because it was competing against crap (VB and Perl). Sun failed to compete when they needed to.

    Cheers,
    Clinton


    Exactly

  9. Back to top

    Re: Is it really needed?

    by Diego Visentin

    Often we talk about closures about simply logic over a collections when this logic is expressable with a single function.
    So maybe it's sufficient something like FunctionalJ:
    functionalj.sourceforge.net/

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.