Search code examples
ddmd

D2: switch statement and variables


In "The D Programming Language" book I see the following:

Usually the case expressions are compile-time constants, but D allows variables, too, and guarantees lexical-order evaluation up to the first match.

Code:

void main()
{
   string foo = "foo";
   string bar = "bar";

   string mrX;

   switch (mrX)
   {
      case foo:
         writeln(foo);
         break;
      case bar:
         writeln(bar);
         break;
      default:
         writeln("who knows");
   }
}

Result:

Error: case must be a string or an integral constant, not foo

What's wrong?

PS. I use DMD32 D Compiler v2.053


Solution

  • It appears to be a bug. It works just fine with variables of type int. I've reported the bug for you: http://d.puremagic.com/issues/show_bug.cgi?id=6176