final switch and straight integers
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Tue Apr 19 07:53:18 PDT 2016
I was surprised that in my code, I had a final switch on an integer, and
compiler never complained.
I looked at the rules here:
http://dlang.org/spec/statement.html#final-switch-statement
Rules are:
1. No DefaultStatement is allowed.
2. No CaseRangeStatements are allowed.
3. If the switch Expression is of enum type, all the enum members
must appear in the CaseStatements.
4. The case expressions cannot evaluate to a run time initialized
value.
What if expression is not of enum type? Apparently that just means no
default statement is required.
In other words:
void foo(int x)
{
final switch(x) {
case 0:
break;
}
}
compiles. I was surprised about this, apparently it just means throw an
error if no case is matched. I don't see a lot of value in this over a
straight switch statement. Is there something I'm missing?
I find this inconsistent with the enum version -- shouldn't I have to
handle all possible values? I think either rule 3 should remove the
qualifier "If the expression is of enum type" and qualify rule 2 which
only would make sense for enums, or we should do away with requiring
handling all enum cases.
-Steve
More information about the Digitalmars-d
mailing list