Wouldn't it be nice (case range statements)

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 14 14:33:34 PDT 2014


On Tuesday, 14 October 2014 at 21:29:59 UTC, John Colvin wrote:
> You have to duplicate the case keyword when declaring case 
> ranges. Why?
>
> Case ranges are inclusive at both ends of the range, unlike in 
> foreach. Again, why?

It comes from writing:

switch(foo) {
   case 1:
   case 2:
   case 3:
   case 4:
      // code
}

Then just replacing the case 2 and case 3 with .. collapsing the 
repetition to just the beginning and the end. If you write it as:

switch(foo) {
   case 1:
   ..
   case 4:
      // code
}

vertically, that is, I think the rationale becomes a lot more 
clear. I like this a lot, it works brilliantly for me and makes 
good sense.

> Allow the second `case` keyword to be removed, which would then 
> have the same semantics as the range in foreach.

That would be ok too.


More information about the Digitalmars-d mailing list