Should the 2.054 feature about warning on implicit fallthrough

Timon Gehr timon.gehr at gmx.ch
Wed Sep 14 15:53:02 PDT 2011


On 09/15/2011 12:43 AM, bearophile wrote:
> Timon Gehr:
>
>> What would you suggest?
>
> At the moment I suggest nothing, because the situation is set.
>
> Case syntax was discussed a lot, by me too. I suggested to differentiate the syntax, not using ".." because in D they denote an interval open on the right.
>
>
>> 'C switch' means 'jump table'. It does do that perfectly. ;)
>
> C language is full of badly designed parts :-)
>
>
>> What is your point?
>
> Well, I don't understand the error it gives :-) Are you able to explain it to me?
>
>
> import std.stdio;
> void main() {
>       int i = 1;
>       switch(i) {
>           case 0:
>               writeln("case 0");
>               goto default; // needed here
>           aLabel:
>               writeln("a label");
>           default:
>               writeln("default");
>               // But always falls through here
>       }
> }
>
> test.d(10): Error: switch case fallthrough - use 'goto default;' if intended
>
> Bye,
> bearophile


import std.stdio;
void main() {
      int i = 1;
      switch(i) {
          case 0:
              writeln("case 0");
              // goto default; // NOT needed here (for it to compile)
          aLabel:
              writeln("a label");
              goto default; // explicit fall through
          default:
              writeln("default");
              // But always falls through here
      }
}

And that is exactly the same as this:


import std.stdio;
void main() {
      int i = 1;
      switch(i) {
          case 0:
              writeln("case 0");
              writeln("a label");
              goto default; // comment this out to get your error back.
          default:
              writeln("default");
      }
}


It is a simple case of switch case fallthrough.










More information about the Digitalmars-d-learn mailing list