Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closures in Java 7 [duplicate]

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.oracle.com/mr/entry/rethinking_jdk7

edit3: There’s not a moment to lose!: http://blogs.oracle.com/mr/entry/quartet

like image 565
Schildmeijer Avatar asked Oct 24 '08 13:10

Schildmeijer


3 Answers

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.

like image 110
Steven Huwig Avatar answered Oct 04 '22 06:10

Steven Huwig


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.

like image 42
Jon Skeet Avatar answered Oct 04 '22 06:10

Jon Skeet


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

Update

Closures (AKA lambdas expressions) in Java 7 didn't happen. They were finally added in the first release of Java 8 in 2014.

like image 32
Dónal Avatar answered Oct 04 '22 06:10

Dónal