Switch: Case Range Syntax

Vijay Nayar madric at gmail.com
Wed Aug 17 13:51:54 PDT 2011


On Wed, 17 Aug 2011 16:29:19 -0400, bearophile wrote:

> Jacob Carlborg:
> 
>> D should have a built-in range type. One that supports syntax for both
>> including and excluding the last element:
>> 
>> auto a = 3 .. 5
>> auto b = 3 ... 5
>> 
>> Then we wouldn't need a special range syntax for switch statements.
> 
> Regarding just switches, GCC has a non standard syntax extension that
> allows you to write case 'a' ... 'z':
> http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_5.html#SEC90
> 
> But it also says:
> 
>> Note: Always write spaces around the ..., for otherwise it may be
>> parsed wrong when you use it with integer values. For example, write
>> this: case 1 ... 5:
>> rather than this:
>> case 1...5:
> 
> Bye,
> bearophile

That's pretty clever.  They recognized the ambiguity problem of 
misinterpreting a string like "5...5" as either "5. .. 5", "5 .. .5" or 
something else by requiring spaces around the '...' operator.

Quoting from the above link:
> Be careful: Write spaces around the ..., for otherwise it may be parsed
> wrong when you use it with integer values. For example, write this:
>   case 1 ... 5:
> rather than this:
>   case 1...5:

Or maybe I'm just easily impressed :)  What can I say, it was the death-
by-a-thousand-cuts in C++ that pushed to find a better compiled language 
in the first place.

 - Vijay


More information about the Digitalmars-d mailing list