August 15, 2011
Steven Schveighoffer:

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

A specific syntax, usable just for delegates that are not void seems to solve some problems:

{ a => a * foo(a) } // OK
{ a, b => a * b } // OK
{ int a, int b => a * b } // OK
{ a => a * 6; }  // syntax error
{ a => return a * 6 }  // syntax error
{ a, b => a *= 6; a * b }  // syntax error?

Bye,
bearophile
August 15, 2011
On Mon, Aug 15, 2011 at 1:57 PM, Andrei Alexandrescu < SeeWebsiteForEmail@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: ...- --- .. -..;


August 15, 2011
On 08/15/2011 11:15 PM, Andrew Wiley wrote:
> On Mon, Aug 15, 2011 at 1:57 PM, Andrei Alexandrescu
> <SeeWebsiteForEmail@erdani.org <mailto:SeeWebsiteForEmail@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?

August 15, 2011
On 8/15/2011 2:00 PM, Jonathan M Davis wrote:
> 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.

Struct destructors and postblits execute arbitrary code. A function returning such a struct would build the return value in the caller's stack.

Overloading comes when a lambda is passed as a template argument, becoming a function pointer, which is then used as an argument to an overloaded function.

August 16, 2011
On 2011-08-15 22:06, Walter Bright wrote:
> 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).

You mean that instead of void it would return something? I'm working quite a lot in Ruby, which automatically returns the last expression in all methods and lambdas. I'm certain I'm implicitly returning a lot of values that I don't know about and in D these functions would be declared "void". I have no problem in Ruby with this, it's working great.

-- 
/Jacob Carlborg
August 16, 2011
On 2011-08-15 22:57, Andrei Alexandrescu 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).
>
> Andrei

Make two semicolons a syntax error.

-- 
/Jacob Carlborg
August 16, 2011
On 2011-08-15 22:06, Walter Bright wrote:
> 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).

Don't know if this apply in this case but couldn't all lambdas/delegates that return something be implicitly converted to delegate/lambda of the same signature but that returns void instead. I would be like calling the delegate but not doing anything with the return value.

-- 
/Jacob Carlborg
August 17, 2011
== Quote from Walter Bright (newshound2@digitalmars.com)'s article
> 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).

Ignoring uninteresting return values in functions performed for side-effects is idiomatic in every language that has both return values and side-effects. How often do people actually look at the return value of printf?

In the unusual case where it is important to disambiguate, the function can always be declared void, or an explicit return statement can still be used. The less ceremony necessary to define a function, the better, IMO.

Cheers, Pillsy
1 2 3
Next ›   Last »