goto a no-go?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Oct 2 20:37:27 PDT 2013


On 10/2/13 8:14 PM, deadalnix wrote:
> On Thursday, 3 October 2013 at 02:21:19 UTC, Andrei Alexandrescu wrote:
>> On 10/2/13 10:06 AM, Dicebot wrote:
>>> On Wednesday, 2 October 2013 at 16:12:58 UTC, Jesse Phillips wrote:
>>>>    if (word.length)
>>>>        scope(exit) FormatOutput();
>>>>
>>>> This is the same as
>>>>
>>>>    if (word.length)
>>>>        FormatOutput();
>>>>
>>>> The `if` introduces a new scope, thus running the code right away.
>>>
>>> Oops, shame on me. Too many `static if`s in my life :(
>>
>> I think it's a common mishap. Might be nice if the compiler disallowed
>> gramatically an unbraced if/while/etc containing only one scope
>> statement.
>>
>> Andrei
>
> Or more generally cope statement at the end of a scope.

I'm cautious about that; that's why I specified "unbraced". Consider:

if (lily)
{
     scope(exit) writeln("Lily was here.");
     // fun();
}

This code is a plausible edit of work that was meaningful and in which 
the programmer has temporarily commented out the call to fun. If the 
compiler would obnoxiously protest that the edited code can't compile, 
that may be more aggravation than win for the user.

In contrast, this is no simple edit of any sensible code:

if (lily)
     scope(exit) writeln("Lily was here.");

Here, the compiler is much more within its rights to demand a code 
change, either to

scope(exit) if (lily) writeln("Lily was here.");

or

if (lily) writeln("Lily was here.");

or

if (lily)
{
     scope(exit) writeln("Lily was here.");
}


Andrei



More information about the Digitalmars-d mailing list