Switch

Rainer Deyke rainerd at eldwood.com
Mon May 18 20:13:39 PDT 2009


Georg Wrede wrote:
> This is usable, easy to read -- and the programmer has no problem to
> remember that .. works differently in case statements than in ranges.

You're making two assumptions here:
1. That inclusive ranges are preferable inside 'case' statements.
2. That non-inclusive ranges are preferable outside 'case' statements.

I don't buy it.  The issue of inclusive versus non-inclusive ranges is
*exactly the same* in and outside 'case' statements.

// Non-inclusive:
foreach (c; start .. middle) doA(c);
foreach (c; middle .. end) doB(c);
foreach (c; start .. end) {
  switch (c) {
    case start .. middle:
      doA(c);
      break;
    case middle .. end:
      doA(c);
      break;
  }
}

// Inclusive:
foreach (c; 'a' ... 'z') doSomething(c);
switch (c) {
  case 'a' ... 'z':
    doSomething(c);
    break;
}
foreach (c; 0 ... int.max) doSomething(c);
switch (c) {
  case 0 ... int.max:
    doSomething(c);
    break;
}


Since I don't accept your assumptions, I see no point in arguing whether
or not those assumptions would justify overloading the '..' operator to
have one meaning in 'case' statements and another meaning elsewhere.


-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list