Java 7 中的闭包

发布于 2024-07-08 08:51:38 字数 675 浏览 12 评论 0 原文

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

这个语法会是什么样子?

我在某处读到,在 java 中引入闭包比 java 5 中的泛型变化更大。这是真的吗? 优点和缺点?

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

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

edit2:重新思考 JDK7: http://blogs.oracle.com/mr/entry/rethinking_jdk7

edit3:刻不容缓!:http://blogs.oracle.com/先生/条目/四重奏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

吻风 2024-07-15 08:51:38

看一下 http://www.javac.info/

看起来就像这样:

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

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

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.

九公里浅绿 2024-07-15 08:51:38

这实际上取决于引入什么,以及是否会引入。 有许多不同规模的关闭提案。

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

就我个人而言,我很乐意看到闭包 - 它们美丽且非常有帮助 - 但我担心有些提案相当棘手。

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.

猫卆 2024-07-15 08:51:38

2009 年 11 月,这个问题出现了令人惊讶的大转变,现在将关闭被添加到 Java 7 中。Java

7 中的更新

闭包(又名 lambda 表达式)没有发生。 它们最终被添加到 2014 年的 Java 8 第一个版本中。

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.

忆梦 2024-07-15 08:51:38

不幸的是,您在 Java 7 中找不到闭包。如果您正在寻找一种更轻的解决方案来在 Java 中使用闭包,请立即查看 lambdaj 项目:

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

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/

巷雨优美回忆 2024-07-15 08:51:38

这是java 7功能http://tech.puredanger.com/java7/#switch 这些例子非常有用。

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

似狗非友 2024-07-15 08:51:38

请注意,“函数类型”实际上是提案中的一种类型:

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

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
岁月静好 2024-07-15 08:51:38

我认为关于最终使用什么语法仍然存在很多争论。 事实上,如果这确实进入 Java 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.

梦中的蝴蝶 2024-07-15 08:51:38

如果没有任何类型的推断,闭包将会变得烦人地冗长……:(

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

要走就滚别墨迹 2024-07-15 08:51:38

关闭有一些严重的边缘情况。 我想说的是,闭包是一个比泛型更重要的变化,而且后者仍然有一些毛茸茸的边缘情况。
例如,如果没有警告,则无法编写/编译 Java Collections 库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文