Wish: Variable Not Used Warning

superdan super at dan.org
Fri Jul 11 11:44:55 PDT 2008


Robert Fraser Wrote:

> Don Wrote:
> 
> > superdan wrote:
> > > 4. warning - switch statement has no default
> > > 
> > > another example of a motherfuck. just require total coverage. in closed-set cases i routinely write anyway:
> > > 
> > > switch (crap) 
> > > {
> > > case a: ...; break;
> > > case b: ...; break;
> > > default: assert(crap == c): ...; break;
> > > }
> > > 
> > > again: vast majority of code already has a default. the minority just has to add a little code. make it an error.
> > 
> > Yup. Make it an error.
> 
> I agree with everything else, but this one I think shouldn't be an error or 
> warning (the implicit assert(0) is enough). This is because the vast 
> majority of switch statements I use (and many I see) are over enums, and 
> if every branch in the enumeration is covered, a pointless "default" will just
> complicate code.

you are not disagreeing. switching over an enum is already closed if you mention all cases. the compiler knows that. it should indeed just throw an error if you have an out-of-range value that you forged from an int. but that's an uncommon case. don't make all pay for a rare bug.

> The "final switch" thing mentioned at the conference & now forgotten, 
> OTOH, is a great idea for statically checking switch statements.

yarp i liked it too til i realized all switches should be final.



More information about the Digitalmars-d mailing list