Closures in Java 7 [duplicate]

帅比萌擦擦* 提交于 2020-01-09 06:20:52

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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/




回答5:


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




回答6:


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



回答7:


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.




回答8:


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




回答9:


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.



来源:https://stackoverflow.com/questions/233579/closures-in-java-7

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