switch()

Walter Bright newshound2 at digitalmars.com
Sun Feb 16 12:03:54 PST 2014


On 2/16/2014 7:42 AM, Manu wrote:
> 1. case fall-through is not supported; explicit 'goto case n;' is required.
> With this in mind, 'break' is unnecessary. Why is it required?

It's that way to prevent confusion from people used to C/C++, and/or 
transliterating code from such to D.


> 2. 'case 1, 3, 7, 8:' is awesome! ...but ranged cases have a totally different
> syntax: 'case 1: .. case 3:'
>
> Why settle on that syntax? The inconsistency looks kinda silly when they appear
> together in code.
> Surely it's possible to find a syntax that works without repeating case and ':'?

Many syntaxes were proposed for that, and this was the best one.


> I'm not sure it's reasonable to use the '..' syntax in this case for that
> reason. '..' is an [) range, case ranges must be [] so that it makes sense when
> dealing with enum key ranges.

That was the known problem with using a..b


> 3. Why is 'default' necessary? If I'm not switching on an enumerated type, then
> many values are meaningless. requiring an empty 'default: break;' line at the
> end is annoying and noisy.

It originally was not required, but there was a campaign by a lot of D users to 
make it required to deal with the common bug of adding a value in one switch 
statement but forgetting to add it to another corresponding one.


> if not for these strange decisions (purely for legacy compliance?).

I hope you'll find them less strange now.



More information about the Digitalmars-d mailing list