Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java use -> instead of => for lambda functions?

Tags:

java

lambda

I am a .NET and JavaScript developer. Now I am working in Java, too. In .NET LINQ and JavaScript arrow functions we have =>.

I know Java lambdas are not the same, but they are very similar. Are there any reasons (technical or non technical) that made java choose -> instead of =>?

like image 409
Dan Nguyen Avatar asked Dec 13 '20 04:12

Dan Nguyen


People also ask

Why -> is used in Java?

Basically, the -> separates the parameters (left-side) from the implementation (right side). (Parameters) -> { Body } where the -> separates parameters and lambda expression body.

What is -> called in Java?

To support lambdas, Java has introduced a new operator “->”, also known as lambda operator or arrow operator. This arrow operator is required because we need to syntactically separate the parameter from the body.

Is arrow function same as lambda expression?

JavaScript arrow functions are roughly the equivalent of lambda functions in python or blocks in Ruby. These are anonymous functions with their own special syntax that accept a fixed number of arguments, and operate in the context of their enclosing scope - ie the function or other code where they are defined.

Why we go for lambda expressions in Java?

Easy-to-Use APIs and Libraries: An API designed using lambda expressions can be easier to use and support other API. Enables support for parallel processing: A lambda expression can also enable us to write parallel processing because every processor is a multi-core processor nowadays.


1 Answers

On September 8, 2011, Brian Goetz of Oracle announced to the OpenJDK mailing list that the syntax for lambdas in Java had been mostly decided, but some of the "fine points" like which type of arrow to use were still up in the air:

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

After considering a number of alternatives, we decided to essentially adopt 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 not yet come to a decision on method reference syntax.

On September 27, 2011, Brian posted another update, announcing that the -> arrow would be used, in preference to C#'s (and the Java prototype's) usage of =>:

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

He goes on to provide some description of the rationale considered by the committee:

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 undesirable interactions, while preserving the overall goal of "mostly looks like what lambdas look like in other similar languages."

Howard Lovatt replied in approval of the decision to prefer ->, writing that he "ha[s] had trouble reading Scala code". Paul Benedict of Apache concurred:

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

Paigan Jadoth chimed in, too:

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

In summary, then, after considering arguments on both sides, the committee felt that consistency with other languages (=> is used in Scala and C#) was less compelling than clear differentiation from the equality operators, which made -> win out.

But Lieven Lemiengre was skeptical:

Other languages (such as Scala or Groovy) don't have this problem because they 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 agree this 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 will be used a lot!
(And I don't think replacing '=>' with '->' will help a lot)

Other than Lieven, I didn't see anyone who criticized the choice of -> and defended => replying on that mailing list. Of course, as Brian predicted, there were almost certainly opinions on both sides, but ultimately, a choice just has to be made in these types of matters, and the committee made the one they did for the stated reasons.

like image 164
Cody Gray Avatar answered Nov 15 '22 01:11

Cody Gray