Possible enhancement: Concise return statements

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 15 12:17:33 PDT 2011


On Mon, 15 Aug 2011 15:00:03 -0400, Walter Bright  
<newshound2 at digitalmars.com> 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.)

Not that I'm for or against this issue, I think comparing this proposal to  
the for + empty statement is completely disproportionate.

What is going to happen if someone adds an extra ';' ?  Compiler error  
("no return statement")

What is going to happen if someone accidentally does not put a semicolon  
on the last statement?  If it's not the correct return type, it's an  
error, otherwise, it's likely what the person wanted.  It's not like a  
return can happen in the middle, it *only* comes in to play as the last  
line of the function.

I agree with others that if this were to be implemented, only allowing the  
short form on single-expression functions would be a good conservative  
start.  But again, I don't frequently use delegates like this, so I'm  
somewhat neutral.

-Steve


More information about the Digitalmars-d mailing list