Lambda Syntax Survey
- Share
-
- |
Read later
Reading List

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.
The lambda syntax is under discussion again at lambda-dev mail list, but this time, they're actively courting opinions.
The “strawman” syntax provided a temporary means to discuss the aspects of the implementation and details of how lambda and type inference would work in the Java language, but it was never intended as a permanent syntax. Now, four separate syntax variations have been proposed:
- Strawman:
#(arglist)(expr)
and#(arglist){statements}
- BGGA:
{ args -> statements }
- SotL:
#{ args -> statements}
- Redmond:
(args) -> { statements }
The poll also provides what using real-life expressions might be like with the above syntaxes:
Example of Strawman: list.filter( #(Foo t)(t.length() > 3) ) .map( #(Foo t)(t.barCount) ) .max(); Example of BGGA: list.filter( { Foo t -> t.length() > 3 } ) .map( { Foo t -> t.barCount } ) .max(); Example of SotL: list.filter(#{ Foo t -> t.length() > 3 } ) .map(#{ Foo t -> t.barCount } ) .max(); Example of Redmond: list.filter((Foo t) -> { t.length() > 3 } ) .map((Foo t) -> { t.barCount } ) .max();
Please see the original post at the lambda-dev mail list for more details on the proposals, and for how to respond to the survey.
Rate this Article
- Editor Review
- Chief Editor Action
Hello stranger!
You need to Register an InfoQ account or Login or login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think
Voting rules
by
Dmitry Gutov
You might make them a service by mentioning that in the article.
Re: Voting rules
by
N T
SotL is good, BGGA is ok, Srawman and Redmond are terrible
by
Ronald Miura
I like SotL more than BGGA, because the # makes it immediately clear that it is a block of executable code, not an expression. Also, the # sign could be consistent with a possible alternate 'method reference' syntax:
Istead of using
List<String> trimmedList = list.map(#{ String s -> StringUtils.trim(s) });
since StringUtils has the same signature as the lambda, one could instead write
List<String> trimmedList = list.map(StringUtils#trim);</string></string>
New Syntax
by
Ali Ebrahimi
seperate syntax for expresion lambdas and statement lambdas :
1) Expression Lambdas:
1.1) if len(args) <= 1: optional parentheses
[(] arg0* [)] -> expr
sample1:
list.filter( t -> t.length() > 3 )
.map( t -> t.barCount )
.max();
sample2: empty args
doRun( -> out.println("lambda"))
1.2) if len(args) > 1: required parentheses
(args ) -> expr
Sample:
Sam2<Integer,Integer,Integer> func5 = (x, y) => x * y;
2) Statement lambdas:
2.1) if len(args) <= 1: optional parentheses
[(] arg0* [)] -> { statement* }
list.filter( t -> t.length() > 3 )
.map( t -> t.barCount )
.max();
doRun( -> { out.println("lambda"); })
2.2) if len(args) > 1: required parentheses
(args ) -> { statement* }
Sam2<Integer,Integer,Integer> func5 = (x, y) => { return x * y;};
Ali Ebrahimi</integer,integer,integer><></integer,integer,integer><>
Re: SotL is good, BGGA is ok, Srawman and Redmond are terrible
by
Dmitry Gutov
The arrow already tells us it's lambda, we don't need the hash sign for that.
Re: SotL is good, BGGA is ok, Srawman and Redmond are terrible
by
Ronald Miura
The # symbol makes it easy to parse, not only by the compiler - which is pretty irrelevant - but by the reader (we always have to parse code mentally to understand it, no matter how close to english it looks).
Re: New Syntax
by
serge ----
they told us that the required parentheses could be removed. Your example is the same
syntax as c# changing => to ->.
Sam2<Integer,Integer,Integer> func5 = (x, y) -> x * y;
Sam2<Integer,Integer,Integer> func5 = (x, y) -> { return x * y;};</integer,integer,integer></integer,integer,integer>
Re: New Syntax
by
Ali Ebrahimi
In C# for zero-len aguments parentheses is not optinal.
test( -> x * y ); //compile fails
Sam0<Integer> func = -> { return x * y }; //compile fails
</integer>
Re: New Syntax
by
Gabriel Ščerbák
I like the idea of separating expression and block lambdas.
My try:
list.filter((String prizeDescription) {
return prizeDescription.length() > 3; // block syntax
}).map((String prize) -> Integer.valueOf(prize)) // expression syntax
.max();
From given options I would stick with BGGA.
Re: New Syntax
by
Dmitry Gutov
And aside from optional brackets, your suggestion is pretty much Redmond style.
Re: New Syntax
by
serge ----
why the syntax you propose would not have been an option (there are alot of discussions behind closed doors).
Working with c# on a regular basis I can say with great confidence that the syntax is very easy
to read and would fit quite naturally in the Java lanaguage (would not look alien).
BGGA requires the least amount of extra characters
by
Faisal Waris
Sotl is ok but I don't like strawman or redmond
Re: New Syntax
by
Richard Clayton
Re: New Syntax
by
Ali Ebrahimi
from(customers).
where( c -> c.orders.size() > 0 ).
orderBy( c -> s.name ).
select(c -> c.name); // select(c -> list(c.name,c.address));
Re: New Syntax
by
Jonathan Allen
Re: New Syntax
by
William Smith
For heaven's sake, just make the lambda syntax identical to C#. I think most people in Enterprise Architecture have to learn both languages (and C# has stolen so much from Java). Why not pilfer something from C#?
For heaven's sake, just make the lambda syntax identical to C#. I think most people in Enterprise Architecture have to learn both languages (and C# has stolen so much from Java). Why not pilfer something from C#?
Well Redmond is the same syntax as C#, of course, so it isn't as if the option is being considered. Also more-or-less the same as the syntax in Scala. BGGA is based on Groovy's syntax, and is, to my mind, slightly nicer to code in though I honestly don't think it makes much odds. The question really is do we have more C#/Scala people or more Groovy people coding in Java today.
On the other hand the strawman syntax has the advantage of being easier to read - the # screams "this is a Lambda" at you, which some people seem to like. As far as I can tell SotL is BGGA with a # glued on the front - I can't see why you would do this. Any Lambda expert care to comment.
This poll was intended for those who have followed the dev list
by
Javier Diaz
If you want to know how a car *will* handle in traffic, then ask the
physicists. If you already have the car (which we do not) then ask
the drivers.
And if your question is on how to design the car, certainly do not ask
the drivers.
Re: New Syntax
by
Richard Clayton
For instance, in the example above(Redmond):
list.filter((Foo t) -> { t.length() > 3 } )
.map((Foo t) -> { t.barCount } )
.max();
In C#, this would be:
list.filter((Foo t) => { t.length() > 3 } )
.map((Foo t) => { t.barCount } )
.max();
In general, however, if there is only one expression, you leave out the curly braces:
list.filter((Foo t) => t.length() > 3 )
.map((Foo t) => t.barCount )
.max();
Re: New Syntax
by
Ali Ebrahimi
list.filter( t -> t.length() > 3 )
.map( t -> t.barCount )
.max();
readability
by
Jörg Buchberger
#{ args -> statements}
from my POV SotL is more readable than any other examples mentioned above, because it sticks out more clearly from (non-functional) surrounding code
because both "#" and "->" are new operators and thus easier to recognize
and because the curly braces make it clear that one deals with code executable later on (as in anonymous code blocks - thus more consistent with existing syntax)