Mitch Pronschinske is the Lead Research Analyst at DZone. Researching and compiling content for DZone's research guides is his primary job. He likes to make his own ringtones, watches cartoons/anime, enjoys card and board games, and plays the accordion. Mitch is a DZone Zone Leader and has posted 2576 posts at DZone. You can read more from them at their website. View Full User Profile

Java 8 Lambda syntax (closures) is decided. It's similar to C# and Scala

09.12.2011
| 32494 views |
  • submit to reddit
Looks like Brian Goetz and the JDK just made a major syntax decision for closures (lambdas) in Java 8: 
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.

The C# syntax is:

   lambda = ArgList Arrow Body
   ArgList = Identifier
             | "(" Identifier [ "," Identifier ]* ")"
             | "(" Type Identifier [ "," Type Identifier ]* ")"
   Body = Expression
             | "{" [ Statement ";" ]+ "}"

Here are some examples of lambda expressions using this syntax:

   x => x + 1
   (x) => x + 1
   (int x) => x + 1
   (int x, int y) => x + y
   (x, y) => x + y
   (x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }
   () => { System.out.println("I am a Runnable"); }

The decision to choose this syntax was twofold:
  - The syntax scores "pretty well" on most subjective measures (though 
has cases where it looks bad, just like all the others do).  In 
particular, it does well with "small" lambdas that are used as method 
arguments (a common case), and also does well with large 
(multi-statement) lambdas.

  - Despite extensive searching, there was no clear winner among the 
alternatives (each form had some good aspects and some really not very 
good aspects, and there was no form that was clearly better than the 
others).  So, we felt that it was better to choose something that has 
already been shown to work well in the two languages that are most like 
Java -- C# and Scala -- rather than to invent something new.

A compiler implementation should be available soon.

Thanks for the find Ray Hulha!

Tags:

Comments

Gregor Kovač replied on Mon, 2011/09/12 - 8:59am

Old news

Jonathan Fisher replied on Mon, 2011/09/12 - 9:50am

OH GOOD JAVA FINALLY HAS LAMBDA'S! OMG!!! I CAN FINALLY USE JAVA!


Sorry guys, Lambda expressions are an over-hyped language feature. There are so many other issues that need to be fixed with the Standard Java API. I'd love to see this dropped in favor of making things like "DateFormatException" a RuntimeException.

Mason Mann replied on Mon, 2011/09/12 - 2:52pm in response to: Jonathan Fisher

That's immensely idiotic, Fisher. Go educate yourself as a garbage guy.

Jacek Furmankiewicz replied on Mon, 2011/09/12 - 3:51pm

I want to see how this works with the collections API. To have a map()/reduce() with lambdas on all Java collections is long overdue. This will be a great addition to the language. Will probably take a lot of wind out other JVM languges, if done properly.

Artur Biesiadowski replied on Tue, 2011/09/13 - 5:11am

@Jacek

 You can get a taste of upcoming interfaces at

http://cr.openjdk.java.net/~mduigou/lambda-sams/4/webrev/

There are no defender methods for the actual collections, but we can probably imagine most of them given the interfaces above

filter with Predicate

map with Mapper

foreach with Block

fold with Reducer

Raw ThinkTank replied on Tue, 2011/09/13 - 7:33am

its always better to build upon the shoulders of the previous giants.

Raw ThinkTank replied on Tue, 2011/09/13 - 7:46am in response to: Jonathan Fisher

Dear lawrence FISHberg,

 

Evolution  morpheus, evolution, think about a time in future when your lambda bytecode will be send accross wire to another machine and it ececutes there, fastforward a bit more and you get multiple virtual machines each one on pre core, now think what if you could execute your array of lambdas on different VMs executing on different cores asynchronously,

 

evolution takes time, not everyone is at right place and right time.

John David replied on Thu, 2012/01/26 - 3:13am

Java has always something exciting to make it my first choice.

Really happy to hear the new features in Java 8.

I always prefer Java on other programming languages.

Java Eclipse

Sadas Dsada replied on Sat, 2012/09/29 - 3:47pm

Here is a closure implementation for Java 5, 6, and 7

http://mseifed.blogspot.se/2012/09/closure-implementation-for-java-5-6-and.html

It contains all one could ask for... I think it is freaking awesome!

Gym Prathap replied on Sat, 2013/05/04 - 7:56am

Lambda expression allows the compiler to infer the type of the variable based on the context

Best Java Training in Chennai 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.