Improvement to switch-case statement
Daniel Keep
daniel.keep.lists at gmail.com
Wed Dec 31 18:46:00 PST 2008
Michel Fortin wrote:
> On 2008-12-31 18:22:32 -0500, "Adam D. Ruppe"
> <destructionator at gmail.com> said:
>
>> This wouldn't be any less efficient than any other switch statement;
>> writing
>>
>> case 2..5:
>> code;
>> break;
>>
>> could just be magically converted into
>>
>> case 2:
>> case 3:
>> case 4:
>> case 5:
>> code;
>> break;
>
> Except that if you want to keep consistency with array slices, 2..5
> should probably not include 5 in the range, thus should be equivalent
> "case 2,3,4". Unfortunatly, that's generally not what you want when
> creating such cases. Imagine writting:
>
> case 'a'..'{':
> case 'A'..'[':
>
> Concistent, yes; but not very appealing. What you want to be able to
> write is this:
>
> case 'a'..'z':
> case 'A'..'Z':
>
> But then it doesn't work as elsewhere in the language anymore.
>
Python solves this rather elegantly; instead of
if( chr in 2..5 ) ...
you can just do this:
if( 2 <= chr <= 5 ) ...
which allows you to explicitly control which ends are inclusive/exclusive.
-- Daniel
More information about the Digitalmars-d
mailing list