No case ranges in final switches?
Jonathan M Davis
jmdavisProg at gmx.com
Sun Mar 6 18:34:53 PST 2011
On Sunday 06 March 2011 18:26:26 Bekenn wrote:
> On 3/6/2011 1:11 PM, bearophile wrote:
> > Do you know why final switches disallow case ranges? Case ranges are not
> > bug-prone:
> >
> >
> > void main() {
> >
> > ubyte u;
> > final switch (u) {
> >
> > case 0: .. case 100:
> > break;
> >
> > case 101: .. case 255:
> > break;
> >
> > }
> >
> > }
>
> 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.
Yeah. While I can see how you might want to use a final switch here, final
switches were really intended for enums, and I'm not sure that they really work
properly with anything else (I'm not even sure that they work properly with
enums - IIRC last time I tried to use one, it didn't enforce anything; hopefully
that's changed by now though).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list