Improvement to switch-case statement
John Reimer
terminal.node at gmail.com
Wed Dec 31 15:21:47 PST 2008
Hello Yigal,
> Mike James wrote:
>
>> Just an idea that could improve Ds switch-case statement - add an
>> elipsys as in object Pascal to allow case ranges.
>>
>> switch (var) {
>> case 1:
>> \\ do something
>> break;
>> case 2..10:
>> \\ do something else
>> break;
>> }
>> What do people think?
>>
>> -=mike=-
>>
> vote--
>
> 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.
>
> I'd personally prefer pattern matching to be added to D and the switch
> to be deprecated and eventually removed from the language.
>
Well, the D switch statement has already abandoned the low-level C construct
in that it allows for switching on strings. I don't think there is a reason
to believe that adding the range feature would mean a loss of efficiency.
I have to agree that this would appear to be a fairly simple and useful feature
that exists in other "old" languages like Pascal and derivitives. I'm not
sure why it was left out in D, but I wouldn't be a bit surprised if the request
has been made before.
On the other hand, it probably wouldn't be hard to implement a range as a
compile time template (or plain) function inserted after the case statement:
case range(2,10): //
But then again... since array slices make use of "..", it seems a shame that
it can't also be used in the case statement for a similar purpose.
-JJR
More information about the Digitalmars-d
mailing list