Improvement to switch-case statement
bearophile
bearophileHUGS at lycos.com
Wed Dec 31 16:17:26 PST 2008
Adam D. Ruppe:
> case 2..5:
> code;
> break;
>
> could just be magically converted into
> case 2:
> case 3:
> case 4:
> case 5:
> code;
> break;
Note that the following syntax is already possible in D:
case 2,3,4,5:
code;
break;
But the range gets larger that syntax gets ugly, so the .. range looks like a good thing to have. So I'm +1 on the idea of the original poster.
The range syntax of the D2 foreach may be even extended to support:
if (x in 10..20) {...
That more or less equals to the C macro:
#define InRange(x, a, b) (unsigned((x) - (a)) <= (b) - (a))
That as you can see has just one branch instead of the usual two:
http://smallcode.weblogs.us/checking_if_point_belongs_to_interval
Bye,
bearophile
More information about the Digitalmars-d
mailing list