Switch-case made less buggy, now with PATCH!

Don nospam at nospam.com
Sat Nov 21 22:55:19 PST 2009


Chad J wrote:
> Don wrote:
>> Things like
>>
>> case A:
>> case B:
>>       foo();
>>       break;
>>
>> do not involve fallthrough bugs. If there's a bug in that code at all,
>> it's that case A doesn't do anything. Empty case statements are not
>> bug-prone.
>>
> 
> My intent is not to forbid these.  I suppose I've tried to damage them
> as little as possible   Letting them slip through without notation would
> do better on that count.  It's a trade-off between that and easier
> (general) fallthrough syntax.

If you think general fallthrough syntax is important, you've 
misunderstood the argument for this feature. See below.

> With this syntax the code required to convert C-style switch-case to
> D-style becomes much easier.  You can just look for case and replace :
> with !:.  Otherwise you have to figure out where to put the goto case
> statement, which is usually easy enough for a human, but can be tricky
> for a program that doesn't have a D parser.  Unless you are OK with
> generating some garbage:

I think you've just created the strongest argument AGAINST this feature: 
that it makes it too hard for machine-generated code.  Forget the !: 
hack. No chance.

>> The thing is, that with the "goto case" syntax, D already has support
>> for explicit fallthrough. No new syntax is required.
>>
> 
> I know this.  Walter knows this.  Why hasn't it been given emphatic
> thumbs up?
> 
> I suspect it's because he's either busy with more important things or
> really enjoys fallthrough.

He made it very, very clear that he thinks that legitimate fallthrough 
is common. If he's right, then this whole thing is a bad idea.

With something like this, you're really wasting your time making a patch 
for it. Patches are only worthwhile in the cases where Walter hasn't 
said anything, or in which he made a positive comment but nothing has 
happened for a long time. Once he's said something negative about a 
feature, providing a patch is not going to change his mind.

BTW, it's _really_ difficult to get an unsolicited patch into DMD. It's 
happened about twice ever.

> Given how easy it is to remove implicit fallthrough and given that he's
> expressed a strong fondness of the fallthrough behavior, I suspect it's
> more the latter than the former.
> 
> At any rate, I've decided to solve both.  So here's a patch to make it
> even easier to enact (especially docs + lib migration), and also a
> syntax to keep fallthrough easy and compact where desired.
> 
> If the syntax is a bad idea, then it can be ditched, and I'll make a new
> patch.  All Walter has to do is say the word.

The argument for it comes down to this: "fallthrough is very rare and 
therefore, most uses of fallthrough are bugs".  If many modifications to 
(say) Phobos are required, then the argument for this feature is false.
 From a comment someone made previously, there were about three 
instances of it in Phobos. If you found you needed to make many changes 
than that, I'll switch sides to Walter's camp. But I think you've just 
gone about this wrong way. Although, the "auto-generated code" argument 
would now need to be addressed.



More information about the Digitalmars-d mailing list