View mode: basic / threaded / horizontal-split · Log in · Help
August 15, 2011
Re: Possible enhancement: Concise return statements
On Mon, 15 Aug 2011 15:00:03 -0400, Walter Bright  
<newshound2@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
August 15, 2011
Re: Possible enhancement: Concise return statements
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?

-- 
/Jacob Carlborg
August 15, 2011
Re: Possible enhancement: Concise return statements
On 2011-08-15 21:17, Steven Schveighoffer wrote:
> On Mon, 15 Aug 2011 15:00:03 -0400, Walter Bright
> <newshound2@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

The reason I don't use delegates as much as I would like to is just 
because of things like this, i.e. the syntax is too verbose.

-- 
/Jacob Carlborg
August 15, 2011
Re: Possible enhancement: Concise return statements
On 8/15/2011 12:17 PM, Steven Schveighoffer wrote:
> What is going to happen if someone adds an extra ';' ? Compiler error ("no
> return statement")

Such lambdas are often used with 'auto' returns, so you'd get no compiler error, 
but a subtle in the way your program works.
August 15, 2011
Re: Possible enhancement: Concise return statements
On 8/15/2011 12:19 PM, Jacob Carlborg wrote:
> Can't we always automatically return the last expression, even if it ends with a
> semicolon?

It interferes with auto return typing (such as void returns).
August 15, 2011
Re: Possible enhancement: Concise return statements
On Mon, 15 Aug 2011 16:05:31 -0400, Walter Bright  
<newshound2@digitalmars.com> wrote:

> On 8/15/2011 12:17 PM, Steven Schveighoffer wrote:
>> What is going to happen if someone adds an extra ';' ? Compiler error  
>> ("no
>> return statement")
>
> Such lambdas are often used with 'auto' returns, so you'd get no  
> compiler error, but a subtle in the way your program works.

So you're saying it becomes a void function?  This still is not bad:

1. Caller is expecting it not to be void, compiler error
2. Caller is not using the return value, who cares?

-Steve
August 15, 2011
Re: Possible enhancement: Concise return statements
On 8/15/11 10:05 PM, Walter Bright wrote:
> On 8/15/2011 12:17 PM, Steven Schveighoffer wrote:
>> What is going to happen if someone adds an extra ';' ? Compiler error
>> ("no
>> return statement")
>
> Such lambdas are often used with 'auto' returns, so you'd get no
> compiler error, but a subtle in the way your program works.

Do you have an actual example for this in mind? In every situation where 
you actually use the return value, »changing its type to void« would 
give you tons of compiler errors. If you are not using the return value 
anyway – who cares?

David
August 15, 2011
Re: Possible enhancement: Concise return statements
On 8/15/2011 1:13 PM, Steven Schveighoffer wrote:
> On Mon, 15 Aug 2011 16:05:31 -0400, Walter Bright <newshound2@digitalmars.com>
> wrote:
>
>> On 8/15/2011 12:17 PM, Steven Schveighoffer wrote:
>>> What is going to happen if someone adds an extra ';' ? Compiler error ("no
>>> return statement")
>>
>> Such lambdas are often used with 'auto' returns, so you'd get no compiler
>> error, but a subtle in the way your program works.
>
> So you're saying it becomes a void function? This still is not bad:
>
> 1. Caller is expecting it not to be void, compiler error
> 2. Caller is not using the return value, who cares?

With struct destructors, (2) is a problem. Overloading can also be a problem.
August 15, 2011
Re: Possible enhancement: Concise return statements
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).

Andrei
August 15, 2011
Re: Possible enhancement: Concise return statements
On Monday, August 15, 2011 13:31 Walter Bright wrote:
> On 8/15/2011 1:13 PM, Steven Schveighoffer wrote:
> > On Mon, 15 Aug 2011 16:05:31 -0400, Walter Bright
> > <newshound2@digitalmars.com>
> > 
> > wrote:
> >> On 8/15/2011 12:17 PM, Steven Schveighoffer wrote:
> >>> What is going to happen if someone adds an extra ';' ? Compiler error
> >>> ("no return statement")
> >> 
> >> Such lambdas are often used with 'auto' returns, so you'd get no
> >> compiler error, but a subtle in the way your program works.
> > 
> > So you're saying it becomes a void function? This still is not bad:
> > 
> > 1. Caller is expecting it not to be void, compiler error
> > 2. Caller is not using the return value, who cares?
> 
> With struct destructors, (2) is a problem. Overloading can also be a
> problem.

It would be a big problem with regard to functions in general IMHO, but how do 
struct destructors and overloading apply to lambdas? It at least _seems_ like 
it would be possible to make it so that single-statement lambdas which have no 
return or ; but return the result of that statement would work without any 
ambiguities.

- Jonathan M Davis
1 2 3
Top | Discussion index | About this forum | D home