Thread overview
[Issue 11213] New: Simplify switch case-range statement
Oct 09, 2013
Andrej Mitrovic
Oct 09, 2013
Andrej Mitrovic
Oct 10, 2013
Kenji Hara
Oct 10, 2013
Andrej Mitrovic
October 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213

           Summary: Simplify switch case-range statement
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-10-09 16:19:04 PDT ---
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:
}
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-10-09 16:19:55 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213


rswhite4@googlemail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rswhite4@googlemail.com


--- Comment #2 from rswhite4@googlemail.com 2013-10-09 16:51:03 PDT ---
Did you read that?
http://dlang.org/faq.html#case_range

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2013-10-09 21:12:33 PDT ---
> http://dlang.org/faq.html#case_range

As written in the FAQ, it's intended syntax.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra@gmail.com


--- Comment #4 from monarchdodra@gmail.com 2013-10-10 02:23:23 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #5 from bearophile_hugs@eml.cc 2013-10-10 03:32:08 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11213


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |WONTFIX


--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-10-10 06:23:15 PDT ---
I don't buy that argument. A switch statement is special enough that inclusive semantics for the right-hand side would not be confusing.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------