October 02, 2013
On Wednesday, 2 October 2013 at 06:19:24 UTC, Jakob Ovrum wrote:
> On Tuesday, 1 October 2013 at 17:15:34 UTC, Ali Çehreli wrote:
>> Nobody mentioned the use of goto with switch-case.
>
> Those are goto-case statements, not goto statements. I don't think it's a useful notion to associate the two beyond the fact that they share a keyword in their syntax.

Well, it remains a "go to this piece of code" command.

Now, you can write a program as a single giant switch that jumps around from state to state, without it ever ending. Like in one of those "read your own adventure books"

int page = 1;
switch(1)
{
    case 1:
    writeln("you arrive in a large chamber...
                To light a torch, go to page 196)
                To run through, go to page 155";
    readfln("%s", &page);
    goto page;

    etc...
}

That's a pretty sweet construct actually :)
October 02, 2013
On Wednesday, 2 October 2013 at 08:15:04 UTC, monarch_dodra wrote:
> That's a pretty sweet construct actually :)

No it isn't, that is awful code.
October 02, 2013
On 01/10/2013 13:36, monarch_dodra wrote:
> I guess I can always use the "do{}while(false);" pattern, but I actually
> find it *more* confusing (IMO)

Although it might not get used that often, it might be nice if D allowed omitting the 'while(false)' part, just using a semi-colon for 'do' termination:

do {
  ...
  if (cond) break; // exit do
  ...
  // implicit fall through, no looping unless 'continue' is used
};

October 02, 2013
On Tuesday, 1 October 2013 at 11:26:54 UTC, Dicebot wrote:
>> // ...
>> if (word.length == 1) goto FormatOutput;
>>
>> // if word.length > 1, some extra work has to be done
>> // initialize some variables, parse, do some processing etc.
>>
>> FormatOutput:
>> // .....
>>
>> return output;
>
> Have never felt need to use `goto` since got familiar with `scope(something)` : http://dpaste.dzfl.pl/ca40b3b6
>
> ;)

    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.
October 02, 2013
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 :(
October 02, 2013
On Wednesday, 2 October 2013 at 17:06:40 UTC, 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 might be a common mistake? I remember making a similar error by placing a scope(exit) in a for loop, hoping they'd all get run when leaving the function.
October 03, 2013
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
October 03, 2013
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.
October 03, 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

October 03, 2013
On Thursday, 3 October 2013 at 03:37:27 UTC, Andrei Alexandrescu wrote:
> 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.
>

It is also fairly common to add control flow that make code unrechable in dev (or when playing with static ifs). And it is disallowed.