Implicit return syntax error

Mike Swingler swingler at apple.com
Tue Dec 15 22:15:27 PST 2009


On Dec 15, 2009, at 9:42 PM, Mark Reinhold wrote:

>> Date: Fri, 11 Dec 2009 09:16:06 +1030
>> From: jason.height at gmail.com
>
>> I just started reading the strawman proposal and i would like to  
>> discuss the
>> benefit/reasons of having an implict return statement for the simple
>> expressions. I actually believe for a person with little or no  
>> experience in
>> closures that the statement "#()(return 42;)" rather than "#()(42)"  
>> is far
>> more readable and natural when compared to the existing jls. A  
>> syntax error
>> is be generated if one omits the "return" keyword for a function ie:
>>
>> public int getFortyTwo() {
>>  42;
>> }
>>
>> As such i propose that the straw man proposal should be changed to so
>> implicit returns are syntax errors.
>
> Thanks for your input, but I have to disagree.  I expect that, in  
> actual
> use, many lambda expressions will have expression rather than  
> statement
> bodies, and so insisting upon the more verbose
>
>    #(){ return 42; }
>
> syntax will just make such uses more, well, verbose.
>
> Imagine replacing "42" with a complex expression of your choice, e.g.,
>
>    #(double a, double b, double c)((-b + sqrt(pow(b, 2) - 4 * a *  
> c))/(2 * a))
>
> Does changing this to
>
>    #(double a, double b, double c){ return (-b + sqrt(pow(b, 2) - 4  
> * a * c))/(2 * a); }
>
> increase readability, or decrease it?

It may be terser by a few characters, but it's an impedance mismatch  
with the rest of the language as it exists today, which reduces  
understandability. I'd also counter that I'd love to rewrite a ton of  
event handlers using lambdas, and most would be multi-statement.

I think it's important to introduce a powerful new concept like lambda  
expressions in terms that are as familiar and consistent as possible  
to the widest audience. Later, it may make sense to propose implicit  
return statements for the language in all contexts, and not just a one- 
off special case for lambdas.

Regards,
Mike Swingler


More information about the lambda-dev mailing list