Comment #0 by andrej.mitrovich — 2013-10-09T16:19:04Z
The current case-range syntax is rather awkward and verbose:
-----
import std.stdio;
void main()
{
foreach (x; 0 .. 10)
{
switch (x)
{
case 0: .. case 4:
writeln("a");
break;
case 5: .. case 9:
writeln("b");
break;
default:
}
}
}
-----
I suggest we allow the syntax "case 0 .. 4:", e.g.:
-----
switch (x)
{
case 0 .. 4:
writeln("a");
break;
case 5 .. 9:
writeln("b");
break;
default:
}
-----
Comment #1 by andrej.mitrovich — 2013-10-09T16:19:55Z
And in fact every time I have to use a case-range statement I forget that we use this strange syntax, I always reach for "case 0 .. 4" but end up having a syntax error. Allowing it would be consistent with the rest of the language.
(In reply to comment #3)
> > http://dlang.org/faq.html#case_range
>
> As written in the FAQ, it's intended syntax.
As an enhancement, I don't see why
case 0 : .. case 4 :
couldn't also be:
case 0 .. 5;
If anything, "case 0 .. 5" is more consistent with what the language does *everywhere*else*, and "case 0 : .. case 4 :" is the actual artifact that never should have existed.
Comment #5 by bearophile_hugs — 2013-10-10T03:32:08Z
(In reply to comment #4)
> As an enhancement, I don't see why
>
> case 0 : .. case 4 :
>
> couldn't also be:
>
> case 0 .. 5;
>
> If anything, "case 0 .. 5" is more consistent with what the language does
> *everywhere*else*, and "case 0 : .. case 4 :" is the actual artifact that never
> should have existed.
This introduces the problem that we'll need to solve adding "[]" to iota:
iota!"[]"(ubyte.min, ubyte.max)
So Kenji is right.
Comment #6 by andrej.mitrovich — 2013-10-10T06:23:15Z
I don't buy that argument. A switch statement is special enough that inclusive semantics for the right-hand side would not be confusing.