Reuse 'do' keyword instead of hash sign (#)
Dan Smith
daniel.smith at oracle.com
Mon Jun 20 10:25:19 PDT 2011
To clarify, I'm not necessarily suggesting that 'new' be used. I'm saying the existence of 'new' provides a precedent for prefixing an expression with a keyword ('do', 'lambda', 'fn', etc.)
—Dan
On Jun 20, 2011, at 10:05 AM, Sam Pullara wrote:
> A long time ago I proposed that we actually use new, but that doesn't make that much sense now. Mine was more syntactic sugar for SAMs rather than lambdas:
>
> new (x) { x + 1 }
>
> list.filter( new (t) { t.length > 3}).map(new (t) { t.barCount}).max()
>
> The nice thing about it is that it looked a lot like an anonymous inner class creation with a lot of type inference.
>
> Sam
>
> On Jun 20, 2011, at 9:51 AM, Dan Smith wrote:
>
>> Generally speaking, you're right -- keywords are for statements. But there is one exception: 'new'; arguably, lambda expressions and class instance creations fall into a similar class of expressions.
>>
>> (As a slight digression, I think the 'if' vs. '?' parallel is useful to consider:
>>
>> if (cond) statement1; else statement2;
>> cond ? expr1 : expr2
>>
>> Some points:
>> - The statement form uses keywords; the expression form uses obscure operator characters.
>> - Programmers seem to be comfortable understanding which to use in which contexts.
>> - The expression form is _not_ enclosed in delimiters, and requires some "lookahead" to mentally process.
>>
>> Further generalizations depend on how you feel about '?:'. Perhaps it's a good example to model lambda expressions after. Or perhaps it's a poorly-designed feature that scares too many beginners away, and we shouldn't use it as a model.)
>>
>> —Dan
>>
>> On Jun 20, 2011, at 12:11 AM, Stephen Colebourne wrote:
>>
>>> No from me too.
>>> (Keywords introduce statements in Java, not expressions.)
>>> Stephen
>>>
>>> 2011/6/18 Marcin Wiśnicki <mwisnicki at gmail.com>:
>>>> This is a repost from 2010/8 since apparently now syntax is up for
>>>> discussion but wasn't back then.
>>>> Also I didn't like any proposal from "Syntax poll" thread ;(
>>>>
>>>> You can call it "do" syntax ;)
>>>> I didn't have the time to think it thoroughly or follow recent java7/8
>>>> developments but here it goes:
>>>>
>>>> Would it be possible to change the clumsy '#' operator to 'do' keyword ?
>>>> I.e.:
>>>>
>>>> do()(5)
>>>> do(){return 5;}
>>>> do(int x, int y) { if (x>y) return x; else return y; }
>>>> button.onClick(do(Event e) { println("button clicked"); });
>>>>
>>>> "strawman" syntax example:
>>>>
>>>> list.filter( #(Foo t)(t.length() > 3) )
>>>> .map( #(Foo t)(t.barCount) )
>>>> .max();
>>>>
>>>> becomes:
>>>>
>>>> list.filter( do(Foo t)(t.length() > 3) )
>>>> .map( do(Foo t)(t.barCount) )
>>>> .max();
>>>>
>>>> I find it more readable; using special characters feels a bit
>>>> like perl (noisy), especially when combining multiple lambdas.
>>>> It even makes sense if you parse it as english.
>>>>
>>>> Don't know what to do with anonymous function type but it could remain as '#'.
>>>>
>>>> Pros:
>>>> - more readable
>>>> - makes sense
>>>> - like '#' needs only 1-lookahead to disambiguate from do-while '(' vs '{' [1]
>>>> - did I mention it's more readable ?
>>>>
>>>> Cons:
>>>> - none (that I could think of)
>>>>
>>>> [1] Automatic Resource Management from Coins wanted to use 'do' but I
>>>> see they have switched to 'try' so no problem there.
>>>>
>>>> PS. Please CC me when responding, thanks.
>>>>
>>>>
>>>
>>
>>
>
More information about the lambda-dev
mailing list