gpt4 book ai didi

java - 为什么 Java 使用 -> 而不是 => 来表示 lambda 函数?

转载 作者:行者123 更新时间:2023-12-03 22:56:42 24 4
gpt4 key购买 nike

我是一名 .NET 和 JavaScript 开发人员。现在我也在 Java 中工作。
在 .NET LINQ 和 JavaScript 箭头函数中,我们有 => .
我知道 Java lambda 不一样,但它们非常相似。是否有任何原因(技术或非技术)使 java 选择 ->而不是 => ?

最佳答案

On September 8, 2011, Brian Goetz of Oracle announced to the OpenJDK mailing list Java 中 lambdas 的语法已经基本确定,但是一些“细点”,比如使用哪种类型的箭头仍然悬而未决:

This just in: the EG has (mostly) made a decision on syntax.

After considering a number of alternatives, we decided to essentiallyadopt the C# syntax. We may still deliberate further on the fine points(e.g., thin arrow vs fat arrow, special nilary form, etc), and have notyet come to a decision on method reference syntax.


On September 27, 2011, Brian posted another update ,宣布 ->将使用箭头,而不是 C#(和 Java 原型(prototype))对 => 的使用。 :

Update on syntax: the EG has chosen to stick with the -> form of thearrow that the prototype currently uses, rather than adopt the =>.


他继续对委员会考虑的理由进行了一些描述:

You could think of this in two ways (I'm sure I'll hear both):

  • This is much better, as it avoids some really bad interactions with existing operators, such as:

    x => x.age <= 0;                 // duelling arrows

    or

    Predicate p = x => x.size == 0;  // duelling equals
  • What a bunch of idiots we are, in that we claimed the goal of doing what other languages did, and then made gratuitous changes "just for the sake of doing something different".

Obviously we don't think we're idiots, but everyone can have an opinion :)

In the end, this was viewed as a small tweak to avoid some undesirableinteractions, while preserving the overall goal of "mostly looks likewhat lambdas look like in other similar languages."


Howard Lovatt replied批准倾向于 -> 的决定,写道他“在阅读 Scala 代码时遇到了麻烦”。 Paul Benedict of Apache同意:

I am glad too. Being consistent with other languages is a laudable goal, butsince programming languages aren't identical, the needs for Java can lead toa different conclusion. The fat arrow syntax does look odd; I admit it. Soin terms of vanity, I am glad to see that punted. The equals character isjust too strongly associated with assignment and equality.


Paigan Jadoth也插话:

I find the "->" much better than "=>". If arrowlings at all instead of themore regular "#(){...}" pattern, then something definitely distinct from thegte/lte tokens is clearly better. And "because the others do that" has neverbeen a good argument, anyway :D.


综上所述,在考虑了双方的论点后, 委员会认为与其他语言的一致性(=> 在 Scala 和 C# 中使用)不如与等式运算符的明显区别那么引人注目,这使得 ->胜出 .
但是 Lieven Lemiengre持怀疑态度:

Other languages (such as Scala or Groovy) don't have this problem becausethey support some placeholder syntax.

In reality you don't write "x => x.age <= 0;"But this is very common "someList.partition(x => x.age <= 18)" and I agreethis looks bad. Other languages make this clearer using placeholder syntax"someList.partition(_.age <= 18)" or "someList.partition(it.age <= 18)"I hope you are considering something like this, these little closures willbe used a lot!
(And I don't think replacing '=>' with '->' will help a lot)


除了 Lieven,我没有看到有人批评 -> 的选择。并为 => 辩护在该邮件列表上回复。当然,正如布赖恩预测的那样,几乎可以肯定双方都有意见,但最终,只需要在这些类型的问题上做出选择,委员会出于上述原因做出了他们所做的选择。

关于java - 为什么 Java 使用 -> 而不是 => 来表示 lambda 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65272378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com