No case ranges in final switches?

bearophile bearophileHUGS at lycos.com
Sun Mar 6 18:51:04 PST 2011


Bekenn:

> Final switch is really just meant to be used with enums; since 
> enumeration values don't have to cover the whole range of possible base 
> type values, and enumeration values don't often have a natural ordering, 
> ranges make little sense there.

Thank you for your answer.
If something doesn't have an ordering, then it's right to disallow the case range on it. But I'd like to be able to use case ranges in safer final switches on bytes/ubytes/chars.

-------------------------

Jonathan M Davis:

>Yeah. While I can see how you might want to use a final switch here, final switches were really intended for enums,<

You often say that language features must pull their weight. Artificially restricting the usability of final switches to just enums makes them not pull their weight.

There are times when I'd like to do this, but DMD is buggy here (I don't remember if I have already put this in Bugzilla):

void main() {
    int x = 100;
    final switch (x % 3) {
        case 0: /*...*/ break;
        case 1: /*...*/ break;
    }
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list