Optional brackets around lambda expression
Brian Goetz
brian.goetz at oracle.com
Wed Jun 15 16:38:09 PDT 2011
> To be honest, I'm not sure what Brian specifically meant by "Redmond syntax"
> there. All his examples did, indeed, use braces, but the name implies
> general correspondence to C# syntax, and C# doesn't require them.
The "Redmond" family of syntaxes (all of them are families, not specific
syntaxes, since each admits many dimensions of tweaking, such as nilary
forms, unary forms, required or optional parentheses around arg lists,
same or different form for expression vs statement lambdas, etc) was
intended to convey the general
args arrow { stuff }
form. It is true that both Scala and C# allow you to elide the braces
when { stuff } is a single expression. (I considered that to be in the
same category of tweaks as whether #{ ... } should be the unary form in
Strawman.) The high-order bit here is the lack of delimiters around the
entire lambda expression, opting instead to use an "operator" (the
arrow) with presumably lower precedence.
Recall that the point of the poll was to see if people had a preference
for the overall shape. There are dozens of tweaks that can be applied,
but I can't believe that the difference between eliding the braces
around a single expression would swing popular opinion from "love it" to
"hate it".
> Brian, can you clarify whether the presence of braces in all examples there
> was intentional (i.e. you consider it an integral part of that syntax), or
> it is a discussion point in any further debate (should this syntax be picked
> as preferred)?
>
> On Wed, Jun 15, 2011 at 2:25 PM, Steven Simpson<ss at comp.lancs.ac.uk> wrote:
>
>> On 15/06/11 21:42, Pavel Minaev wrote:
>>> Note that this applies also to "Redmond syntax" in general; e.g.:
>>>
>>> new User().use(x => x + "a string");
>>
>> Ah - I only looked at the options in the original "Syntax poll", where
>> expressions were in braces for Redmond.
>>
>>> On Wed, Jun 15, 2011 at 1:35 PM, Yuval Shavit<yshavit at akiban.com
>>> <mailto:yshavit at akiban.com>> wrote:
>>>
>>> What would happen with something like:
>>>
>>> public interface Sam {
>>> String doSomething(String arg);
>>> }
>>> public class User {
>>> public void use(Sam sam) { System.out.println("saw a sam"); }
>>> public void use(String string) { System.out.println("saw a
>>> string"); }
>>> }
>>>
>>> new User().use( #(x) x + "a string" );
>>>
>>> In that context, which are we passing?
>>> - lambda that takes a String, concatenates "a string" to it and
>>> returns
>>> the result
>>>
>>
>> Yes.
>>
>>> - a string consisting of (lambda x -> x).toString() concatenated
>>> with "a
>>> string"
>>>
>>
>> For that, you write (#(x) x) + "a string" - ordinary expression brackets
>> can still be used, but are only necessary in the more contrived cases.
>>
>>
>>
>
More information about the lambda-dev
mailing list