Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Mitch Pronschinske is the Senior Content Curator (aka. "Lord of the Zones") at DZone. That means he writes and searches for the finest developer content in the land so that you don't have to. He often has hotdogs for lunch, likes to make his own ringtones, enjoys card and board games, and is married to an underwear model. Mitch is a DZone employee and has posted 1711 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
Email
Views: 16160
  • 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:

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gregor Kovac 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: cj91

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: cj91

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

Comment viewing options

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