Optional brackets around lambda expression

Steven Simpson ss at comp.lancs.ac.uk
Thu Jun 16 16:30:06 PDT 2011


On 16/06/11 11:19, Ali Ebrahimi wrote:
> Hi, how about this one:
>
> process( x -> x + 1 );
>  process( x -> { return x + 1; } );
>  process(  x -> {
>   if (x == 0) {
>     return 0;
>   }
>   return x + 1;
>  });

It's arguably more aesthetic.  Is it harder to parse (infix vs prefix)? 
I imagine not significantly so, but parser writers' opinions surely
carry more weight.

I think you need some bracketing if there's more than one parameter:

process( x, y -> x + y );

You'd have to use more than just the grammar to determine that the first
x is not a separate parameter, which would be a Bad Thing, right?  So
one of these, or similar, would be necessary:

process( (x, y) -> x + y );
process( ( x, y -> x + y ) );
process( { x, y -> x + y } ); // closest to current syntax?

Of those, I'd favour the brackets around just the parameters, based on
the same argument that supports "#(params) expr", i.e. low precedence is
only a problem for the most contrived cases.  I'd also rather save the
braces for statement lambdas.

Still thinking of stripping away superfluities, can you get away without
'->', provided you have brackets around the parameters?  I think no, as
"(x) -x" would be troublesome.


More information about the lambda-dev mailing list