Bug 11213 – Simplify switch case-range statement

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-09T16:19:00Z
Last change time
2013-10-10T06:23:15Z
Assigned to
nobody
Creator
andrej.mitrovich

Comments

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.
Comment #2 by rswhite4 — 2013-10-09T16:51:03Z
Comment #3 by k.hara.pg — 2013-10-09T21:12:33Z
> http://dlang.org/faq.html#case_range As written in the FAQ, it's intended syntax.
Comment #4 by monarchdodra — 2013-10-10T02:23:23Z
(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.