A safer switch? + abbreviation of member names in enum switches

Justin Johansson no at spam.com
Mon Oct 12 01:55:07 PDT 2009


bearophile Wrote:

> This is a pruned method from a command-line Java program:
> 
> private static void parseCmdLine(String args[]) {
>...
> }
> 
> 
> I may translate part of that to D like this:
> 
> switch (arg) {
>...
> }
> 
> But the "-h" case misses a break. Languages like C# have found a way to avoid (in most cases) that common bug. D may use some like this:
> ...


(Sorry bearophile, don't mean to steal your thunder but since I'm writing some D1 switch code right now, I may as well strike while the iron is still hot ...)

Speaking of switch statements, when switching on an enum type, e.g.

enum Color {
   RED,
   GREEN,
   BLUE
}

void foo( Color color) {
   switch (color) {
   case Color.RED:
      break;
   case Color.GREEN:
      break;
   case Color.BLUE:
      break;
   }
}

Would it be possible for the compiler to infer the declared enum type, in this case Color, making for abbreviation of the enum member names in the case clauses as per the following?

void bar( Color color) {
   switch (color) {
   case RED:
      break;
   case GREEN:
      break;
   case BLUE:
      break;
   }
}


Koala hugs,

-- Justin Johansson




More information about the Digitalmars-d mailing list