Improvement to switch-case statement

Stewart Gordon smjg_1998 at yahoo.com
Fri Jan 2 16:48:15 PST 2009


BCS wrote:
> Reply to Stewart,
<snip>
>> I'd personally prefer my proposal from years ago to be taken
>> seriously: http://www.digitalmars.com/d/archives/22722.html
>>
>> ISTM silly to improve switch but at the same time keep it restricted
>> to the same old arcane syntax.
>>
>> Stewart.
> 
> I for one don't like that proposal for a few reasons:
> 
> Because it looses fall through and adding it back in with "goto case n;" 
> gets messy in generated code. I use fall through an some of my template 
> code.

Please read that proposal properly.  You'll see that it supports 
something more general than fall through.  Unless your primary reason 
for using switch is to implement Duff's device or something like that.

Notice also that nowhere did I propose removing the old switch syntax. 
So if you still want it, it's still there.

> Because it can lead to hard to understand code.

I'm sure you could, if you want, write code that is just as obfuscated 
with the switch we have.

> you need to examine all 
> cases to find the code path rather than just find a single case with the 
> given value.

No I don't.  Once I've found a path that matches, I need only to examine 
those remaining that aren't preceded by else in order to determine 
whether there are any more.

> Allowing the same case in more than one place could lead to some 
> extremely hard to find bugs where the same case is added more than once 
> in error.

Only if you carry on thinking in terms of C switch.  If you think of it 
as syntactic sugar for if, then it might be easier.

Stewart.



More information about the Digitalmars-d mailing list