Improvement to switch-case statement

BCS ao at pathlink.com
Fri Jan 2 09:40:20 PST 2009


Reply to Stewart,

> Yigal Chripun wrote:
> <snip>
>> IMO, either the switch statement should remain the low level
>> (effiecient) construct as in C, or replaced by a general mechanism of
>> pattern matching. your solution is kinda between the two above
>> options and I don't see the point of it.
>> 
> What would a "general mechanism of pattern matching" involve, and how
> would it replace switch?  Would it replace if as well?
> 
>> I'd personally prefer pattern matching to be added to D and the
>> switch to be deprecated and eventually removed from the language.
>> 
> 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.
Because it can lead to hard to understand code. you need to examine all cases 
to find the code path rather than just find a single case with the given 
value.
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.





More information about the Digitalmars-d mailing list