Improvement to switch-case statement

Nick Sabalausky a at a.a
Thu Jan 1 09:45:02 PST 2009


"Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message 
news:gjhapq$171v$1 at digitalmars.com...
>
>
> 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.
>

Not a bad idea. How does it handle something like this:

if( a > b < c)

Is that an error? Or does it get split into:

if( a > b && b < c)





More information about the Digitalmars-d mailing list