October 01, 2013
On Tuesday, 1 October 2013 at 16:55:12 UTC, Manu wrote:
> On 1 October 2013 23:54, deadalnix <deadalnix@gmail.com> wrote:
>
>> On Tuesday, 1 October 2013 at 11:40:35 UTC, Manu wrote:
>>
>>> Note: there's an un-handled case in your example, but I'll ignore that.
>>> Anyway, goto is supported. Walter likes it. I use it from time to time.
>>> I'd say 90% of the time I find goto useful is when I need to bail from
>>> nested loops. I've often wondered if something like break(2) would be a
>>> more elegant solution to the breaking out of nested loops problem.
>>>
>>>
>> BreakableLoop: while(condition) {
>>     while(condition) {
>>         // Stuff . . .
>>         break BreakableLoop;
>>     }
>> }
>>
>> Also works with continue.
>>
>
> ... O_O
>
> Is this D code?

Yup :)

> I've never seen that. If that works, that's awesome!

Yup :)
October 01, 2013
On Tuesday, 1 October 2013 at 16:55:12 UTC, Manu wrote:
> On 1 October 2013 23:54, deadalnix <deadalnix@gmail.com> wrote:
>
>> On Tuesday, 1 October 2013 at 11:40:35 UTC, Manu wrote:
>>
>>> Note: there's an un-handled case in your example, but I'll ignore that.
>>> Anyway, goto is supported. Walter likes it. I use it from time to time.
>>> I'd say 90% of the time I find goto useful is when I need to bail from
>>> nested loops. I've often wondered if something like break(2) would be a
>>> more elegant solution to the breaking out of nested loops problem.
>>>
>>>
>> BreakableLoop: while(condition) {
>>     while(condition) {
>>         // Stuff . . .
>>         break BreakableLoop;
>>     }
>> }
>>
>> Also works with continue.
>>
>
> ... O_O
>
> Is this D code?
> I've never seen that. If that works, that's awesome!

Yes, I use it quite a lot ! Very useful construct.
October 01, 2013
On Tuesday, 1 October 2013 at 16:55:12 UTC, Manu wrote:
> Is this D code?
> I've never seen that. If that works, that's awesome!

http://dlang.org/statement.html#BreakStatement

"If break is followed by Identifier, the Identifier must be the label of an enclosing while, for, do or switch statement, and that statement is exited. It is an error if there is no such statement."
October 01, 2013
Nobody mentioned the use of goto with switch-case.

  http://ddili.org/ders/d.en/switch_case.html

goto can appear in three ways under case sections:

* 'goto case' causes the execution to continue to the next case.

* 'goto default' causes the execution to continue to the default section.

* 'goto expression' causes the execution to continue to the case that matches that expression.

The following program demonstrates these three uses by taking advantage of a foreach loop:

import std.stdio;

void main()
{
    foreach (value; [ 1, 2, 3, 10, 20 ]) {
        writefln("--- value: %s ---", value);

        switch (value) {

        case 1:
            writeln("case 1");
            goto case;

        case 2:
            writeln("case 2");
            goto case 10;

        case 3:
            writeln("case 3");
            goto default;

        case 10:
            writeln("case 10");
            break;

        default:
            writeln("default");
            break;
        }
    }
}

The output:

--- value: 1 ---
case 1
case 2
case 10
--- value: 2 ---
case 2
case 10
--- value: 3 ---
case 3
default
--- value: 10 ---
case 10
--- value: 20 ---
default

Ali

October 01, 2013
On 10/1/2013 4:22 AM, Chris wrote:
> Or does the compiler recognize _obvious_ cases and generate code
> accordingly? For example would it turn something like this

Actually, the dmd compiler turns ALL control flow into goto statements internally, then the optimizer works on that. For example, it reverse engineers loops from a graph of basic blocks connected by goto's.
October 01, 2013
On Tuesday, 1 October 2013 at 17:15:34 UTC, Ali Çehreli wrote:
> Nobody mentioned the use of goto with switch-case.
>
>   http://ddili.org/ders/d.en/switch_case.html
>
> goto can appear in three ways under case sections:
>
> * 'goto case' causes the execution to continue to the next case.
> [SNIP]
> Ali

Hum... What is the difference between "goto case" and "continue" in a switch statement?
October 01, 2013
On Tuesday, 1 October 2013 at 19:45:20 UTC, monarch_dodra wrote:
> On Tuesday, 1 October 2013 at 17:15:34 UTC, Ali Çehreli wrote:
>> Nobody mentioned the use of goto with switch-case.
>>
>>  http://ddili.org/ders/d.en/switch_case.html
>>
>> goto can appear in three ways under case sections:
>>
>> * 'goto case' causes the execution to continue to the next case.
>> [SNIP]
>> Ali
>
> Hum... What is the difference between "goto case" and "continue" in a switch statement?

Never-mind, I seem to have been confused. I thought "continue" was the keyword to use for explicit fall-through.

Carry on then.
October 01, 2013
On Tuesday, 1 October 2013 at 18:47:27 UTC, Walter Bright wrote:
> On 10/1/2013 4:22 AM, Chris wrote:
>> Or does the compiler recognize _obvious_ cases and generate code
>> accordingly? For example would it turn something like this
>
> Actually, the dmd compiler turns ALL control flow into goto statements internally, then the optimizer works on that. For example, it reverse engineers loops from a graph of basic blocks connected by goto's.

Most compiler do that.
October 02, 2013
On 2 October 2013 03:00, deadalnix <deadalnix@gmail.com> wrote:

> On Tuesday, 1 October 2013 at 16:55:12 UTC, Manu wrote:
>
>> On 1 October 2013 23:54, deadalnix <deadalnix@gmail.com> wrote:
>>
>>  On Tuesday, 1 October 2013 at 11:40:35 UTC, Manu wrote:
>>>
>>>  Note: there's an un-handled case in your example, but I'll ignore that.
>>>> Anyway, goto is supported. Walter likes it. I use it from time to time. I'd say 90% of the time I find goto useful is when I need to bail from nested loops. I've often wondered if something like break(2) would be a more elegant solution to the breaking out of nested loops problem.
>>>>
>>>>
>>>>  BreakableLoop: while(condition) {
>>>     while(condition) {
>>>         // Stuff . . .
>>>         break BreakableLoop;
>>>     }
>>> }
>>>
>>> Also works with continue.
>>>
>>>
>> ... O_O
>>
>> Is this D code?
>> I've never seen that. If that works, that's awesome!
>>
>
> Yes, I use it quite a lot ! Very useful construct.
>

Super useful! :)
Well, I think there's almost no reason left for goto... I can't think of
any of my common use cases that aren't satisfied with a proper D construct.


October 02, 2013
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.