BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Lambda Syntax Survey

Lambda Syntax Survey

Bookmarks

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

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Voting rules

    by Dmitry Gutov,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

  • Re: Voting rules

    by N T,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: Voting rules

    by N T,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: Voting rules

    by Yetao Chen,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    but I just completed it 1 min before.

  • Re: Voting rules

    by Yetao Chen,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    +1

  • Re: Voting rules

    by Dmitry Gutov,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Yeah, I voted the same.

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

    by Ronald Miura,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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>

  • Voting rules

    by Avelar Leão,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I keep w\ BGGA, usefull.

  • New Syntax

    by Ali Ebrahimi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

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

    by Dmitry Gutov,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

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

    by Ronald Miura,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: New Syntax

    by serge ----,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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>

  • Re: New Syntax

    by Ali Ebrahimi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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>

  • Re: New Syntax

    by Gabriel Ščerbák,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

  • Re: New Syntax

    by Dmitry Gutov,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

  • Re: New Syntax

    by serge ----,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • BGGA requires the least amount of extra characters

    by Faisal Waris,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

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

  • Re: New Syntax

    by Richard Clayton,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: New Syntax

    by Ali Ebrahimi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: New Syntax

    by Jonathan Allen,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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?

  • Re: New Syntax

    by William Smith,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

  • Re: New Syntax

    by Richard Clayton,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: New Syntax

    by Ali Ebrahimi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    with type inference with my proposed syntax would be:


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

  • readability

    by Jörg Buchberger,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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)

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT