www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I define a label inside a switch?

reply "MachineCode" <netorib94 gmail.com> writes:
I used to do it in C but in D it's giving this compile error 
message:

 switch case fallthrough - 'use goto case;' if intended
Here's the code:
 switch(value) {
 // alof of cases here
 // ...
 white: // regular label
			case 'a': case 'c':
			case 'd': case 'k':
				do_something();
				break;
  case 'e':
        do_something2();
         break;
  default: assert(0);
 }
How is it fall through if there's a break? does D switch differ from C in any way?
Dec 14 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 14 Dec 2014 18:24:39 +0000
MachineCode via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 I used to do it in C but in D it's giving this compile error=20
 message:
=20
 switch case fallthrough - 'use goto case;' if intended
=20 Here's the code: =20
 switch(value) {
 // alof of cases here
 // ...
 white: // regular label
			case 'a': case 'c':
			case 'd': case 'k':
				do_something();
				break;
  case 'e':
        do_something2();
         break;
  default: assert(0);
 }
=20 How is it fall through if there's a break? does D switch differ=20 from C in any way?
why do you need that? you can use literally what compiler told you: `goto case 'a';` for example.
Dec 14 2014
parent reply "MachineCode" <netorib94 gmail.com> writes:
On Sunday, 14 December 2014 at 18:27:28 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Sun, 14 Dec 2014 18:24:39 +0000
 MachineCode via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 I used to do it in C but in D it's giving this compile error 
 message:
 
 switch case fallthrough - 'use goto case;' if intended
Here's the code:
 switch(value) {
 // alof of cases here
 // ...
 white: // regular label
			case 'a': case 'c':
			case 'd': case 'k':
				do_something();
				break;
  case 'e':
        do_something2();
         break;
  default: assert(0);
 }
How is it fall through if there's a break? does D switch differ from C in any way?
why do you need that? you can use literally what compiler told you: `goto case 'a';` for example.
The labels are disabled then? I find that goto case case_value ugly and prefer goto labelName; but if it's the only way to go let's do it
Dec 14 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 14 December 2014 at 18:41:54 UTC, MachineCode wrote:
 The labels are disabled then? I find that goto case case_value 
 ugly and prefer goto labelName; but if it's the only way to go 
 let's do it
I'm not sure if it's intentionally not supported, or just an oversight. Probably the former, so you should use "goto case". I agree it is annoying having to type out the expression for which case to go to; I can't remember if there was any rationale given for disallowing such a case.
Dec 14 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 14 Dec 2014 18:48:15 +0000
Meta via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 On Sunday, 14 December 2014 at 18:41:54 UTC, MachineCode wrote:
 The labels are disabled then? I find that goto case case_value=20
 ugly and prefer goto labelName; but if it's the only way to go=20
 let's do it
=20 I'm not sure if it's intentionally not supported, or just an=20 oversight. Probably the former, so you should use "goto case". I=20 agree it is annoying having to type out the expression for which=20 case to go to; I can't remember if there was any rationale given=20 for disallowing such a case.
i believe that this is just an oversight. technically that label belongs to the previous case, so that case is assumed to have some code after `break` or `goto`, even if there is no real code follows. not sure if it worth fixing though.
Dec 14 2014