Closures in Java 7 [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-27 18:46:18

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.

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.

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.

Mario Fusco

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/

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

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

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!