About switch case statements...

Don nospam at nospam.com
Mon Nov 16 00:27:22 PST 2009


Andrei Alexandrescu wrote:
> Don wrote:
>> Andrei Alexandrescu wrote:
>>> Chad J wrote:
>>>> So, switch-case statements are a frequent source of nasty bugs.  Fixing
>>>> them (well) requires breaking backwards compatibility.
>>>>
>>>> Any chance this will happen for D2?
>>>>
>>>> (This is intended as more of a reminder and simple curiosity than a
>>>> discussion.)
>>>
>>>
>>> Walter's answer to that has put me to silence for good. "But I use 
>>> fall-through all the time!" I then knew the feature will never make it.

>> It's actually not something I care about at all. But I think Walter's 
>> wrong about his coding style. And looking at how rarely it's actually 
>> used by someone who thinks he uses it a lot, convinces me that 
>> intentional use of fall-through is much less common than bugs 
>> introduced by leaving out a break statement.
>>
>> An interesting result.
>> But I'd much rather change Walter's mind about opPow() or the 
>> meta.compiles(XXX) proposal.
> 
> Don       <----------        genius

Nah, I just know how Walter's mind works. It takes one to know one...

I frequently use fall-through myself, but having looked at this sample, 
I bet I don't use it nearly as much as I thought: again, "frequently" 
probably means "about 1% of the time". But I *know* I've had bugs from 
leaving out 'break'.

> I understand that you're more interested about the issues that preoccupy 
> you (and incidentally me) the most, but "switch" is a day-to-day 
> programming (and programmer's) workhorse, so a change there might have a 
> wider (and hopefully more positive) impact.

I should have said "immutable array literals" instead.


 >>> I wish very much that a transferring control flow statement (break,
 >>> return, goto etc.) is required at the end of each case. Then, the
 >>> rare case when you want to break through is easy to implement as goto
 >>> case xxx; and all is good.

Requiring 'goto' to implement fall-through would run into the prejudice 
against 'goto'. It's necessary to persuade managers that "goto case 
XXX;" isn't a bad, evil goto that eats babies. I have no idea if that's 
difficult or not. Otherwise, I think it's a superb solution.
(providing that empty fall-through case statements remain valid; 
disallowing them would be really annoying).



More information about the Digitalmars-d mailing list