About switch case statements...

Derek Parnell derek at psych.ward
Sun Nov 15 14:05:40 PST 2009


On Sun, 15 Nov 2009 12:49:01 -0500, 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?

The phrase "snowball in hell" comes to mind.

> (This is intended as more of a reminder and simple curiosity than a
> discussion.)

And in spite of your good intentions (~ not a discussion ~) this will be
one ;-) 

The Euphoria Programming language, which I'm helping to implement, has
'solved' this issue. This language is not a C-derived one so don't be
alarmed at its apparent verbosity.

The default 'case' in a 'switch' is to not fall through; an implied 'break'
is assumed at the end of each 'case' clause. 

  switch Foo do
      case 1 then
         ... whatever for 1 ...
         ... does not fall through to the next case ...

      case 2, 3 then
         ... whatever for 2 and 3 ...
         ... does not fall through to the next case ...

      case 4 then
         ... whatever for 4 ...
         ... does not fall through to the next case ...

  end switch


However, the coder can change the default behaviour on a 'switch' statement
by including the phrase 'with fallthru', thus changing the default
behaviour such that each 'case' clause will fall through to the next case
unless the coder uses an explicit 'break' statement. This, as you can see,
is the DPL behaviour.

  switch Foo with fallthru do
      case 1 then
         ... whatever for 1 ...
         ... falls through to the next case ...

      case 2, 3 then
         ... whatever for 2 and 3 ...
         break   /* explicitly required to prevent falling through */

      case 4 then
         ... whatever for 4 ...
         ... falls through to the next case ...

  end switch


Further more, whatever the default behaviour for the 'switch', it can be
adjusted on any of the 'case' clauses. This means that a by-default-break
switch, can use the 'fallthru' statement to explicitly override the break
default and fall through to the next case. 

  switch Foo do /* by default, break */
      case 1 then
         ... whatever for 1 ...
         fallthru /* explicitly falls through */

      case 2, 3 then
         ... whatever for 2 and 3 ...
         ... implied break here ...

      case 4 then
         ... whatever for 4 ...
         ... implied break here ...

  end switch

In short, Euphoria allows for both ideologies to exist, and in fact to
co-exist in peace. The standard default gives most protection from
mistakes, but for those coders that like to keep on the edge, they can code
with the level of risk that is acceptable to themselves.

I can see good reason for DPL to adopt a similar stance. I would suggest a
new keyword to make things easier for coders (after all, that's why we have
programming languages), but for those who see keywords as evil, I'm sure
that someone can come up with another syntax that avoids a new keyword (at
the expense of clarity) eg. "! break" or the old standby, "static" LOL.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list