goto (outer) case
monarch_dodra
monarchdodra at gmail.com
Tue Feb 19 04:35:02 PST 2013
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;
}
//----
More information about the Digitalmars-d-learn
mailing list