Re: goto (outer) case
I'm feeling the wind from Edsger Dijkstra spinning in his grave... -=mike=- Nick Sabalausky seewebsitetocontac...@semitwist.com wrote in message news:20130218205937.0768@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: XX break; } break; case Foo.b: break; } --- Without adding extra code anywhere else, is there anything I can stick in for XX 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.
Re: goto (outer) case
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: XX break; } break; case Foo.b: break; } --- Without adding extra code anywhere else, is there anything I can stick in for XX 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 XX. 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; } //
goto (outer) case
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: XX break; } break; case Foo.b: break; } --- Without adding extra code anywhere else, is there anything I can stick in for XX 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.
Re: goto (outer) case
On Mon, 18 Feb 2013 20:59:37 -0500, Nick Sabalausky seewebsitetocontac...@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: XX break; } break; case Foo.b: break; } --- Without adding extra code anywhere else, is there anything I can stick in for XX 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
Re: goto (outer) case
On Mon, 18 Feb 2013 22:30:48 -0500 Steven Schveighoffer schvei...@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.
Re: goto (outer) case
On Mon, 18 Feb 2013 22:45:29 -0500, Nick Sabalausky seewebsitetocontac...@semitwist.com wrote: On Mon, 18 Feb 2013 22:30:48 -0500 Steven Schveighoffer schvei...@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