views:

3960

answers:

9
+15  Q: 

Closures in Java 7

I have heard that closures could be introduced in the next Java standard that is scheduled to be released somewhere around next summer.

What would this syntax look like?

I read somewhere that introducing closures in java is a bigger change than generic was in java 5. Is this true? pros and cons?

(By now we definitely know that closures not will be included in the next Java release)

OR

edit: http://puredanger.com/tech/2009/11/18/closures-after-all/ :D

edit2: Re-thinking JDK7: http://blogs.sun.com/mr/entry/rethinking_jdk7

+8  A: 

Have a look at http://www.javac.info/ .

It seems like this is how it would look:

boolean even = { int x => x % 2 == 0 }.invoke(15);

where the { int x => x % 2 == 0 } bit is the closure.

Steven Huwig
Correct me if I'm wrong but a closure needs an open term (read free variable) per se to be a closure :) (?)
Schildmeijer
@Schildmeijer: well, you could consider the example to be a closure enclosing a null lexical environment.
Derrick Turk
Currently it appears that the "=>" is more likely to be "->".
Neal Gafter
+9  A: 

It really depends on what gets introduced, and indeed whether it will be introduced at all. There are a number of closure proposals of varying sizes.

See Alex Miller's Java 7 page for the proposals and various blog posts.

Personally I'd love to see closures - they're beautiful and incredibly helpful - but I fear that some of the proposals are pretty hairy.

Jon Skeet
+3  A: 

This is the java 7 features http://tech.puredanger.com/java7/#switch the examples are very usefull.

Agusti-N
+2  A: 

Note that a "function-type" is really a type under the proposal:

{int => boolean} evaluateInt;    //declare variable of "function" type
evaluateInt = {int x => x % 2 }; //assignment
oxbow_lakes
+2  A: 

I think there is still a lot of debate going in with regards to what syntax will ultimately be used. I'd actually be pretty surprised if this does make it into Java 7 due to all of that.

jsight
+8  A: 

In November 2009 there was a surprising u-turn on this issue, and closures will now be added to Java 7.

Don
That is what I've heard as well. Hence the small language changes (aka Project Coin - http://openjdk.java.net/projects/coin/) project.
Brian Yarger
Project Coin isn't related to closures. You can't call closures a small change, right?
Jorn
You should ignore the comment about Coin, it was made before I edited my answer to say something completely different.
Don
+1  A: 

Closures have some serious edge cases. I would say that Closures are a much more significant change than Generics and the later still has a number hairy edge cases. e.g. The Java Collections libraries cannot be written/compiled without warnings.

Peter Lawrey
I'd say that's more of a problem with Java's implementation of generics than with the concept of generics itself.
bcat
I agree. It now appears that a simplified form of Closures will be in a very delayed Java 7.
Peter Lawrey
+1  A: 

Unofortunately you will not find closure in Java 7. If you are looking for a lighter solution to have closure in java just now check out the lambdaj project:

http://code.google.com/p/lambdaj/

Update: Java 7 will actually have closures.

Mario Fusco
Thank you for the misinformation, Mario. First, lambda expressions are currently slated for Java 7. Second, lambdaj does not provide closures, as it cannot capture variables from the lexically enclosing scope.
Neal Gafter
Hi Neal. If you read the date of my answer it has been posted on September 2009. That means it was 2 months before the announcement of closure in Java 7 at Devoxx. If you remember at that time practically everyone was sure that we won't have closure in Java 7. Do you agree? What should I did after the announcement? Look all over the internet for all the posts where I spoke about closure in Java 7 to correct them?
Mario Fusco
Good point; my mistake. So this answer is not incorrect with respect to the Java 7 status, merely out of date. As for lambdaj, a presumably very useful library, it does provide something like lambda expressions but not closures.
Neal Gafter
+1  A: 

closures will be annoyinglly verbose if there won't be any sort of type inference... :(

Pedro Morte Rolo