Implicit return syntax error

Mark Reinhold mr at sun.com
Tue Dec 15 21:42:53 PST 2009


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

- Mark


More information about the lambda-dev mailing list