Eigenpoll : D wish list : New Switch Case Design 

D should adopt a new design for switch case. The C design is known to cause bugs, simply because the user forgets to add a break statement, or a variable is uninitialized.
The best reason given for keeping the C design is because "it's easier to translate code". However, I don't think this reason is good enough to keep the old design.
D has dropped several features from C and C++ (preprocessors for example), so I don't see why not drop the old switch case design too.


This design is similar to the old design, but it fixes the problems with the old design, and it's easy to translate old code to.


switch(value){
case 0
{
// Do stuff...

// case automatically breaks at end of scope
}
case 10, 20 // same as "case 10: case 20:"
{
fall; // Falls to next case statement
}

// Variables can be defined at the switch scope
int v = 35;
case 30
{
writeln(v); // Prints 35
// Variables are automatically initialized
}

default
{
}
}

From personal experience, you're more likely to break after a case statement than let it fall through. So simply, break should be the default action, and not fall.

Report this item for cleanup