www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1112] New: Allow enums in WithStatements

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1112

           Summary: Allow enums in WithStatements
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: deewiant gmail.com


Been asked for a few times, submitting it here for posterity.

enum Enum {
        A = 0, B, C
}

void main() {
        with (Enum) {
                assert (A == 0);
                assert (B == A+1);
                assert (C == B+1);
        }
}

It would be handy if the above worked. A common use case is switching on an
enum:

enum Enum {
        A = 0, B, C
}

void main() {
        Enum e;
        with (Enum) switch (e) {
                case A:
                case B:
                case C:
                default: break;
        }
        // the above looks much better than:
        switch (e) {
                case Enum.A:
                case Enum.B:
                case Enum.C:
                default: break;
        }
}


-- 
Apr 08 2007
next sibling parent Ary Manzana <ary esperanto.org.ar> writes:
d-bugmail puremagic.com escribió:
 http://d.puremagic.com/issues/show_bug.cgi?id=1112
 
            Summary: Allow enums in WithStatements
            Product: D
            Version: unspecified
           Platform: All
         OS/Version: All
             Status: NEW
           Severity: enhancement
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: deewiant gmail.com
 
 
 Been asked for a few times, submitting it here for posterity.
 
 enum Enum {
         A = 0, B, C
 }
 
 void main() {
         with (Enum) {
                 assert (A == 0);
                 assert (B == A+1);
                 assert (C == B+1);
         }
 }
 
 It would be handy if the above worked. A common use case is switching on an
 enum:
 
 enum Enum {
         A = 0, B, C
 }
 
 void main() {
         Enum e;
         with (Enum) switch (e) {
                 case A:
                 case B:
                 case C:
                 default: break;
         }
         // the above looks much better than:
         switch (e) {
                 case Enum.A:
                 case Enum.B:
                 case Enum.C:
                 default: break;
         }
 }
 
 

In fact, it would be nice to be able to ommit the name of the enum if one is switching an enum value. Enum e; switch(e) { case A: case B: case C: default: break; } That looks much nicer... That's how it is done in Java, BTW. (further, if you switch an enum in Java, it's an error to precede the enum member with the enum name... That's just paraonid!... but it always looks cleaner).
Apr 07 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1112


Alexey Ivanov <aifgi90 gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |aifgi90 gmail.com
         Resolution|                            |WORKSFORME


--- Comment #2 from Alexey Ivanov <aifgi90 gmail.com> 2010-02-14 18:00:46 PST
---
Works with DMD 1.055 and DMD 2.040

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