Java 8 Lambda syntax (closures) is decided. It's similar to C# and Scala
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!
(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
Jonathan Fisher replied on Mon, 2011/09/12 - 9:50am
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
Jacek Furmankiewicz replied on Mon, 2011/09/12 - 3:51pm
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
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