Thread overview
[Issue 5714] New: case ranges in final switches
Dec 02, 2012
Andrej Mitrovic
Dec 02, 2012
Andrej Mitrovic
March 07, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5714

           Summary: case ranges in final switches
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-03-07 04:31:23 PST ---
This is a low-priority enhancement suggestion.

If the type to final-switch on has a natural ordering (so it's not an enum!), like a char/ubyte/byte, then in some situations I'd like to use the range syntax:


void main() {
     ubyte u;
     final switch (u) {
         case   0: .. case 100: break;
         case 101: .. case 255: break;
     }
}


Currently DMD 2.052 gives the errors:
test.d(4): Error: case ranges not allowed in final switch
test.d(5): Error: case ranges not allowed in final switch


See also bug 5713

If case ranges are not allowed on full-range int values, then you can't use them in a final switch.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5714


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-02 05:55:57 PST ---
(In reply to comment #0)
> If case ranges are not allowed on full-range int values, then you can't use them in a final switch.

Basically the compiler just translates this:

case 1: .. case 3: call()

into:

case 1:
case 2:
case 3:
    call()

And it also limits the number of cases to 256 (for optimization purposes?).

So a final switch would only work on byte-types. I'm not sure if Walter would like a feature that only works with one fundamental type. Note also that because `final switch` currently works on ints (personally I would reject such code), people would assume they could add case ranges for the full int type, but they would get the 256 case limitation error instead.

As a current workaround you could use a mixin that generates the case statements and a 'default' which throws a SwitchError.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5714



--- Comment #2 from bearophile_hugs@eml.cc 2012-12-02 06:05:59 PST ---
(In reply to comment #1)

> So a final switch would only work on byte-types. I'm not sure if Walter would like a feature that only works with one fundamental type.

There are also chars, and ubytes.


> `final switch` currently works on ints (personally I would reject such
> code),

It's indeed wrong, there is another enhancement request/bug report that asks for it to be fixed!


> As a current workaround you could use a mixin that generates the case statements and a 'default' which throws a SwitchError.

You can't have a default in static switches.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5714



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-02 06:34:36 PST ---
(In reply to comment #2)
> (In reply to comment #1)
> 
> > So a final switch would only work on byte-types. I'm not sure if Walter would like a feature that only works with one fundamental type.
> 
> There are also chars, and ubytes.

They have the same range so it doesn't matter what you call them, the range is limited to 0 .. 255.

> You can't have a default in static switches.

Yes, you would use a normal switch for that. But you could have a helper template mixin that could error internally if you don't provide all the cases, which is a workaround for final switches not providing case ranges.

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