Java 7 中的闭包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/233579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:49:34  来源:igfitidea点击:

Closures in Java 7

javasyntaxclosuresjava-7

提问by Schildmeijer

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

我听说闭包可能会在计划于明年夏天发布的下一个 Java 标准中引入。

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?

我在某处读到在 java 中引入闭包比在 java 5 中引入闭包是一个更大的变化。这是真的吗?利弊?

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

(现在我们肯定知道闭包不会包含在下一个 Java 版本中)

OR

或者

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

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

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

编辑 2:重新思考 JDK7:http: //blogs.oracle.com/mr/entry/rethinking_jdk7

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

编辑 3:一刻也不能错过!:http: //blogs.oracle.com/mr/entry/quartet

采纳答案by Steven Huwig

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

看看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.

其中{ int x => x % 2 == 0 }位是闭包。

回答by Jon Skeet

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

这实际上取决于引入的内容,以及是否会引入。有许多不同规模的关闭建议。

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

有关提案和各种博客文章,请参阅Alex Miller 的 Java 7 页面

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

就我个人而言,我很想看到闭包——它们很漂亮,而且非常有用——但我担心有些提议很麻烦。

回答by Agusti-N

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

这是 java 7 特性http://tech.puredanger.com/java7/#switch的例子非常有用。

回答by oxbow_lakes

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

回答by jsight

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.

我认为关于最终将使用什么语法仍然存在很多争论。由于所有这些,如果这确实进入 Java 7,我实际上会感到非常惊讶。

回答by Dónal

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

2009 年 11 月,这个问题出现了令人惊讶的掉头,现在Java 7中将添加闭包

Update

更新

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

Java 7 中的闭包(AKA lambdas 表达式)没有发生。它们最终在 2014 年 Java 8 的第一个版本中添加。

回答by Peter Lawrey

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.

关闭有一些严重的边缘情况。我会说闭包是一个比泛型更重要的变化,而后者仍然有一些毛茸茸的边缘情况。例如,Java 集合库不能在没有警告的情况下编写/编译。

回答by 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:

不幸的是,你不会在 Java 7 中找到闭包。

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

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

回答by Pedro Rolo

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

如果没有任何类型的推断,闭包将会非常冗长……:(