A safer switch? + abbreviation of member names in enum switches
bearophile
bearophileHUGS at lycos.com
Mon Oct 12 02:07:50 PDT 2009
Justin Johansson:
> 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;
> }
> }
That's a special case, with the purpose of shortening code a little. So while writing Color.something isn't handy, it safe and it has no special casing. So I don't like your idea a lot.
Related: I have compiled your code with D1 with warnings (-w):
enum Color {
RED,
GREEN,
BLUE
}
void foo(Color color) {
switch (color) {
case Color.RED:
break;
case Color.GREEN:
break;
case Color.BLUE:
break;
}
}
void main() {}
It prints:
warning - test.d(8): Error: switch statement has no default
warning - test.d(8): Error: statement is not reachable
But there's no need for default there because all enum cases are covered. So that warning test has to be improved.
Bye,
bearophile
More information about the Digitalmars-d
mailing list