No more fall through in case statement?

Carlos Santander csantander619 at gmail.com
Thu Jan 3 12:31:55 PST 2008


bearophile escribió:
 > Bill Baxter:
 >> I'm no C# guy but that looks like a nice way to fix C's switch.
 >
 > I think the C switch is broken in 2 or more ways, so I'd like to see D 
improve it. This is some code I have shown time ago:
 >
 > import std.stdio: writefln;
 > void main(string[] args) {
 >   switch (args[1]) {
 >     int x = 1; // NOT initialized?
 >     case "1": writefln("1! x=", x); break;
 >     case "2": writefln("2! x=", x); break;
 >   }
 > }
 >
 > More on switch-induced bugs:
 > http://www.soft.com/AppNotes/attcrash.html
 >
 >
 >> "goto case 5" looks like something straight out of Walter's playbook, too.<
 >
 > I don't like that goto too much... You usually just want do go down (and by 
default you want to go to the end of the switch).
 >
 >
 >> But it's been discussed many times now and it doesn't seem likely Walter's 
going to change it in D.<
 >
 > I agree with Walter when he says it's better that if a syntax that looks like 
C works like C (D goes against this rule few times). So we need a syntax that 
looks different. Instead of switch{} it may be used caseof{}, plus the "default" 
statement, plus a new statement like "fall" (or something similar) that tells 
the compiler just to not break, so it's just the opposite of C :-)
 >
 > As example, this is a part of the std.string.capwords function:
 >
 > switch (s[i]) {
 >     case ' ':
 >     case '\t':
 >     case '\f':
 >     case '\r':
 >     case '\n':
 >     case '\v':
 >         if (inword) {
 >             r ~= capitalize(s[istart .. i]);
 >             inword = false;
 >         }
 >         break;
 >
 >     default:
 >         if (!inword) {
 >             if (r.length)
 >             r ~= ' ';
 >             istart = i;
 >             inword = true;
 >         }
 >         break;
 > }
 >
 >
 > With that syntax it becomes:
 >
 > caseof (s[i]) {
 >     case ' ':  fall;
 >     case '\t': fall;
 >     case '\f': fall;
 >     case '\r': fall;
 >     case '\n': fall;
 >     case '\v':
 >         if (inword) {
 >             r ~= capitalize(s[istart .. i]);
 >             inword = false;
 >         }
 >
 >     default:
 >         if (!inword) {
 >             if (r.length)
 >             r ~= ' ';
 >             istart = i;
 >             inword = true;
 >         }
 > }
 >

How about using existing D syntax?

switch (s[i]) {
      case ' ', '\t', '\f', '\r', '\n', '\v':
          if (inword) {
              r ~= capitalize(s[istart .. i]);
              inword = false;
          }
          break;

      default:
          if (!inword) {
              if (r.length)
              r ~= ' ';
              istart = i;
              inword = true;
          }
          break;
}

 >
 > D already follows such "inversion rule" regerding C in some cases, like here:
 >
 > int a = void;
 >
 > In a safe(r) language the default syntax/behavior *must* be the safer one, 
expecially when this has no/little costs in terms of speed/memory.
 >
 > Bye,
 > bearophile



-- 
Carlos Santander Bernal




More information about the Digitalmars-d mailing list