InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Lambda Syntax Survey

Posted by Alex Blewitt on Jun 10, 2011

Sections
Development
Topics
Java ,
Languages ,
Closures ,
Programming ,
Language ,
Lambda Expressions

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.

  • This article is part of a featured topic series on Java

25 comments

Watch Thread Reply

Voting rules by Dmitry Gutov Posted
Re: Voting rules by N T Posted
Re: Voting rules by N T Posted
Re: Voting rules by Yetao Chen Posted
Re: Voting rules by Dmitry Gutov Posted
Re: Voting rules by Yetao Chen Posted
SotL is good, BGGA is ok, Srawman and Redmond are terrible by Ronald Miura Posted
Re: SotL is good, BGGA is ok, Srawman and Redmond are terrible by Dmitry Gutov Posted
Re: SotL is good, BGGA is ok, Srawman and Redmond are terrible by Ronald Miura Posted
Voting rules by Avelar Leão Posted
New Syntax by Ali Ebrahimi Posted
Re: New Syntax by serge boulay Posted
Re: New Syntax by Ali Ebrahimi Posted
Re: New Syntax by Gabriel Ščerbák Posted
Re: New Syntax by serge boulay Posted
Re: New Syntax by Richard Clayton Posted
Re: New Syntax by Jonathan Allen Posted
Re: New Syntax by William H Posted
Re: New Syntax by Richard Clayton Posted
Re: New Syntax by Ali Ebrahimi Posted
Re: New Syntax by Dmitry Gutov Posted
Re: New Syntax by Ali Ebrahimi Posted
BGGA requires the least amount of extra characters by Faisal Waris Posted
This poll was intended for those who have followed the dev list by Javier Diaz Posted
readability by Jörg Buchberger Posted
  1. Back to top

    Voting rules

    by Dmitry Gutov

    The post also has "voting rules" at the end, and people are asked to vote according to them.
    You might make them a service by mentioning that in the article.

  2. Back to top

    Re: Voting rules

    by N T

    The survey was for *UP TO* 100 people. It is already closed :(

  3. Back to top

    Re: Voting rules

    by N T

    Personally I think BGGA is the best, with Redmond a close second. What does everyone else think?

  4. Back to top

    Re: Voting rules

    by Yetao Chen

    but I just completed it 1 min before.

  5. Back to top

    Re: Voting rules

    by Yetao Chen

    +1

  6. Back to top

    Re: Voting rules

    by Dmitry Gutov

    Yeah, I voted the same.

  7. Back to top

    SotL is good, BGGA is ok, Srawman and Redmond are terrible

    by Ronald Miura

    Strawman and Redmond have too many parentheses. Besides that, they make the parameter declaration and the code block look like separate things. Specially Redmond, it looks like it is an operation between two expressions.

    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>

  8. Back to top

    Voting rules

    by Avelar Leão

    I keep w\ BGGA, usefull.

  9. Back to top

    New Syntax

    by Ali Ebrahimi

    My favorite syntax is AS:

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

  10. Back to top

    Re: SotL is good, BGGA is ok, Srawman and Redmond are terrible

    by Dmitry Gutov

    SotL is the most redundant out of all four.
    The arrow already tells us it's lambda, we don't need the hash sign for that.

  11. Back to top

    Re: SotL is good, BGGA is ok, Srawman and Redmond are terrible

    by Ronald Miura

    The arrow is not much visible, since it uses a combination of common expression symbols.

    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).

  12. Back to top

    Re: New Syntax

    by serge boulay

    this was not even an option. Although, it would have made a huge difference if
    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>

  13. Back to top

    Re: New Syntax

    by Ali Ebrahimi

    Yes, this is improve version of C#'s lambda syntax.
    In C# for zero-len aguments parentheses is not optinal.

    test( -> x * y ); //compile fails

    Sam0<Integer> func = -> { return x * y }; //compile fails
    </integer>

  14. Back to top

    Re: New Syntax

    by Gabriel Ščerbák

    Don't forget about the argument types!

    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.

  15. Back to top

    Re: New Syntax

    by Dmitry Gutov

    Questions like optional brackets around whatever, fat vs thin arrow, etc, are left for later, like Brian wrote in the message.

    And aside from optional brackets, your suggestion is pretty much Redmond style.

  16. Back to top

    Re: New Syntax

    by serge boulay

    I suspect that most people would have identified with the syntax you propose. I have no idea
    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).

  17. Back to top

    BGGA requires the least amount of extra characters

    by Faisal Waris

    Its too late now but I would have voted for BGGA.

    Sotl is ok but I don't like strawman or redmond

  18. Back to top

    Re: New Syntax

    by Richard Clayton

    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#?

  19. Back to top

    Re: New Syntax

    by Ali Ebrahimi

    another sample:


    from(customers).
    where( c -> c.orders.size() > 0 ).
    orderBy( c -> s.name ).
    select(c -> c.name); // select(c -> list(c.name,c.address));

  20. Back to top

    Re: New Syntax

    by Jonathan Allen

    I wouldn't mind if Java starting ripping off C# wholesale. Just copy everything that can fit in the JVM, then we can look into surpass C#. Really, what's the point of having the best VM if the language can't keep up?

  21. Back to top

    Re: New Syntax

    by William H

    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.

  22. Back to top

    This poll was intended for those who have followed the dev list

    by Javier Diaz

    A great answer from Neal, anyway (about the subject of how to poll different populations and how ...):

    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.

  23. Back to top

    Re: New Syntax

    by Richard Clayton

    No, Redmond is similiar, but not exact.

    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();

  24. Back to top

    Re: New Syntax

    by Ali Ebrahimi

    with type inference with my proposed syntax would be:


    list.filter( t -> t.length() > 3 )
    .map( t -> t.barCount )
    .max();

  25. Back to top

    readability

    by Jörg Buchberger

    SotL:

    #{ 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)