Switch-case made less buggy, now with PATCH!

Chad J chadjoan at __spam.is.bad__gmail.com
Sun Nov 22 01:50:19 PST 2009


Don wrote:
> 
> 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.
> 

If that's the strongest argument, then this is cake.  I'll go through
Phobos and insert goto case's by hand if that's what needs to happen.

Most of the fallthrough I saw was stuff like this:

case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
...etc...
break;

Without fallthrough this is easily rewritten as

case '1': .. case '9':

or

case '1', '2', '3', '4', '5', '6', '7', '8', '9':

Not the easiest thing to make a sed expression do, but quite obvious by
hand.  No !: needed.

Anyhow, "hack"?  "No chance."?  You feel quite strongly.

> 
> 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.
> 

I feel this matters enough that I will roll the dice anyways.

> 
> The argument for it comes down to this: "fallthrough is very rare and
> therefore, most uses of fallthrough are bugs".  

Not as I remember.

"inline assembly is very rare and therefore, most uses of inline
assembly are bugs."
Such reasoning wouldn't get inline asm removed either.  Fallthrough is
not bad because it is rare.  I think that's only mentioned because the
rarity makes it easier to let go of fallthrough altogether.

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. 

"needed".  Heck no.  "wanted" would be more appropriate.  I could have
minimized changes much more, but that costs a little in terms of the
language being nice in the long term.

There are way more than three fallthroughs in Phobos2.  Most of it is
empty cases.  Axing fallthrough also axes empty cases, unless you
explicitly say otherwise (and I didn't).  Empty cases can be easily
rewritten into equivalent code that is also clean and pleasing.

I also minded the "default: assert(0);" pattern, quite common, which may
or may not be fallthrough depending upon how smart the compiler wants to
be.  If the compiler wants to be dumb, then this becomes "default:
assert(0); break;" or similar.  I was being conservative and letting it
be dumb.  At least in dmd's case it's easy to recognize "assert(0);" as
unconditional branching, so it need not be dumb.

But I think you've just gone about
> this wrong way. 

Perhaps.  I'll find out for myself.

Also, I'll say it again in a slightly different way:  I'd be fine with
adding no new syntax and making a special case for empty case
statements.  Really I would.

Although, the "auto-generated code" argument would now
> need to be addressed.

Is the auto-generated code thing really important enough to /need/ to
address it?  I know I mentioned it, but I also called it "a minor point".

Man, I add a small nicety and it becomes some kind of uber awesome
argument against the much more important issue.  Sorry, I don't quite
follow the logic.



More information about the Digitalmars-d mailing list