Possible enhancement: Concise return statements
Timon Gehr
timon.gehr at gmx.ch
Mon Aug 15 14:30:42 PDT 2011
On 08/15/2011 11:15 PM, Andrew Wiley wrote:
> On Mon, Aug 15, 2011 at 1:57 PM, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org <mailto:SeeWebsiteForEmail at erdani.org>>
> wrote:
>
> On 8/15/11 2:19 PM, Jacob Carlborg wrote:
>
> On 2011-08-15 21:00, Walter Bright wrote:
>
> On 8/15/2011 3:54 AM, Timon Gehr wrote:
>
> 'When the last ExpressionStatement in a function body is
> missing the
> ';', it is
> implicitly returned.'
>
>
> This has been proposed several times before, it was also
> proposed for
> C++0x. The difficulty is it makes having a ; or not
> substantially alter
> the semantics. The history of these languages is that the
> presence or
> absence of ; can be hard to spot, as in:
>
> for (int i = 0; i < 10; i++);
> ... do this ...
>
> which has cost at least one expert developer I know an
> entire afternoon
> staring at it convinced there was a compiler bug because his
> loop
> executed only once.
>
> (And this is why D disallows this syntax.)
>
>
> Can't we always automatically return the last expression, even if it
> ends with a semicolon?
>
>
> Then two semicolons mean return void :o).
>
>
> If you want void, you have to use this as your last expression:
> ...- --- .. -..;
>
Two semicolons means the last statement is an empty statement, so
Andrei's suggestion would be sensible if the last expression was
implicitly returned all the time ;)
@Topic:
So, I'm not convinced that accidents related to writing one surplus ; or
leaving one ; away would lead to hard to find and impossible to
statically catch bugs. (After all, such accidents would *always* make it
into the type signature of the function.) As this apparently has been
discussed before, does anyone actually have a real world example where
it could be shown to be problematic?
More information about the Digitalmars-d
mailing list