www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - goto (outer) case

reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
Consider these nested switches:

---------------------------
enum Foo {a, b}
enum Bar {bar}

auto foo = Foo.a;
auto bar = Bar.bar;

final switch(foo)
{
case Foo.a:
    final switch(bar)
    {
    case Bar.bar:
        XXXXXX
        break;
    }
    break;

case Foo.b:
    break;
}
---------------------------

Without adding extra code anywhere else, is there anything I can stick
in for XXXXXX to get execution to jump to "case Foo.b:"?

Doing "goto case Foo.b;" doesn't work. It just gives a compile error
that a Foo can't be implicitly converted to Bar.

This ability isn't critical, of course, but it would help clean up some
code I have.
Feb 18 2013
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 18 Feb 2013 20:59:37 -0500, Nick Sabalausky  
<SeeWebsiteToContactMe semitwist.com> wrote:

 Consider these nested switches:

 ---------------------------
 enum Foo {a, b}
 enum Bar {bar}

 auto foo = Foo.a;
 auto bar = Bar.bar;

 final switch(foo)
 {
 case Foo.a:
     final switch(bar)
     {
     case Bar.bar:
         XXXXXX
         break;
     }
     break;

 case Foo.b:
     break;
 }
 ---------------------------

 Without adding extra code anywhere else, is there anything I can stick
 in for XXXXXX to get execution to jump to "case Foo.b:"?

 Doing "goto case Foo.b;" doesn't work. It just gives a compile error
 that a Foo can't be implicitly converted to Bar.

 This ability isn't critical, of course, but it would help clean up some
 code I have.
Hm.. wouldn't plain goto work:
 final switch(foo)
 {
 case Foo.a:
     final switch(bar)
     {
     case Bar.bar:
         goto HORRIBLE_HACK;
         break;
     }
     break;

 case Foo.b:
 HORRIBLE_HACK:
     break;
 }
Not sure, didn't test. -Steve
Feb 18 2013
parent reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Mon, 18 Feb 2013 22:30:48 -0500
"Steven Schveighoffer" <schveiguy yahoo.com> wrote:
 
 Hm.. wouldn't plain goto work:
 
 final switch(foo)
 {
 case Foo.a:
     final switch(bar)
     {
     case Bar.bar:
         goto HORRIBLE_HACK;
         break;
     }
     break;

 case Foo.b:
 HORRIBLE_HACK:
     break;
 }
Not sure, didn't test.
Maybe, but as you say it's a "horrible hack", and since my motivation was for some code cleanup it's just kind of a wash. Ie, if I do that, than it's debatable how much better that is than: final switch(foo) { case Foo.a: bool gotoB; final switch(bar) { case Bar.bar: gotoB = true; break; } if(gotoB) goto case Foo.b; break; case Foo.b: break; } Depends which you have more hatred towards: verbosity or goto. In my case, I ended up just converting the inner switch to an if/else chain, which actually isn't too bad since my inner enum/switch only had three cases, one of which was just an empty "break;". Some sort of "goto outer.case Foo.b;" could be handy, but I suspected there probably wasn't such a thing.
Feb 18 2013
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 18 Feb 2013 22:45:29 -0500, Nick Sabalausky  
<SeeWebsiteToContactMe semitwist.com> wrote:

 On Mon, 18 Feb 2013 22:30:48 -0500
 "Steven Schveighoffer" <schveiguy yahoo.com> wrote:
 Hm.. wouldn't plain goto work:

 final switch(foo)
 {
 case Foo.a:
     final switch(bar)
     {
     case Bar.bar:
         goto HORRIBLE_HACK;
         break;
     }
     break;

 case Foo.b:
 HORRIBLE_HACK:
     break;
 }
Not sure, didn't test.
Maybe, but as you say it's a "horrible hack", and since my motivation was for some code cleanup it's just kind of a wash.
My love for goto is not great. I cringe using goto case even ;) That being said, I have no problem using it when the situation calls for it. If there's one thing I hate more than goto, it's copy-pasting code, especially in the same function. If goto avoids that, I'm for it. Especially in an embedded project. -Steve
Feb 18 2013
prev sibling next sibling parent "Mike James" <foo bar.com> writes:
I'm feeling the wind from Edsger Dijkstra spinning in his grave...

-=mike=-

"Nick Sabalausky" <SeeWebsiteToContactMe semitwist.com> wrote in message 
news:20130218205937.00000768 unknown...
 Consider these nested switches:

 ---------------------------
 enum Foo {a, b}
 enum Bar {bar}

 auto foo = Foo.a;
 auto bar = Bar.bar;

 final switch(foo)
 {
 case Foo.a:
    final switch(bar)
    {
    case Bar.bar:
        XXXXXX
        break;
    }
    break;

 case Foo.b:
    break;
 }
 ---------------------------

 Without adding extra code anywhere else, is there anything I can stick
 in for XXXXXX to get execution to jump to "case Foo.b:"?

 Doing "goto case Foo.b;" doesn't work. It just gives a compile error
 that a Foo can't be implicitly converted to Bar.

 This ability isn't critical, of course, but it would help clean up some
 code I have.
 
Feb 19 2013
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 19 February 2013 at 01:59:45 UTC, Nick Sabalausky 
wrote:
 Consider these nested switches:

 ---------------------------
 enum Foo {a, b}
 enum Bar {bar}

 auto foo = Foo.a;
 auto bar = Bar.bar;

 final switch(foo)
 {
 case Foo.a:
     final switch(bar)
     {
     case Bar.bar:
         XXXXXX
         break;
     }
     break;

 case Foo.b:
     break;
 }
 ---------------------------

 Without adding extra code anywhere else, is there anything I 
 can stick
 in for XXXXXX to get execution to jump to "case Foo.b:"?

 Doing "goto case Foo.b;" doesn't work. It just gives a compile 
 error
 that a Foo can't be implicitly converted to Bar.

 This ability isn't critical, of course, but it would help clean 
 up some
 code I have.
If you break up the contents of the switches into functions, you shouldn't need a goto, you can just insert the function call in XXXXXX. Also, there are (arguably) more or less dirty ways to use goto. If you use it to jump back and forth between random spots and your programs, it becomes horrible blasphemy (eg "HORRIBLE HACK"), and is the main reason goto has such a bad rep. However, you can use it to somewhat "enhance" your control structures in ways that aren't too frowned upon. Most notably, there is the "double break" or "double continue" goto (not needed in D) or the "restart goto". These are (more or less) common, and usually accepted use by those more open minded and comfortable with gotos. In your case, I'd consider using a "restart goto": //---- mySwitch: final switch(foo) { case Foo.a: final switch(bar) { case Bar.bar: //Restart the loop, but as a b: foo = Foo.b; goto mySwitch; //Restarts switch } break; case Foo.b: break; } //----
Feb 19 2013