www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8012] New: Add .length field to enums or provide helper function to Phobos

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

           Summary: Add .length field to enums or provide helper function
                    to Phobos
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: trivial
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



13:44:17 PDT ---
My use-case:

enum Foo
{
    x,
    z,
    y
}

enum Bar : Foo
{
    x = Foo.x,
    y = Foo.y,
    z = Foo.z
}

I really want to ensure that when I update Foo I will be forced to update Bar
(IOW get a compile-time error). If enums had a .length property then I could
easily add a static assert:

static assert(Foo.length == Bar.length);

As a workaround (if the proposal is rejected), we might add this template to
Phobos:

template EnumLength(E)
    if (is(E == enum))
{
    enum EnumLength = [__traits(allMembers, E)].length;
}

Used like so:
static assert(EnumLength!Foo == EnumLength!Bar);

I'd prefer having .length over instantiating templates, but either is fine for
me.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 01 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8012


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|trivial                     |enhancement



13:47:14 PDT ---
Oops I marked it wrong. It's an enhancement request.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 01 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8012


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

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



15:39:37 PDT ---


 
 Dupe of Issue 4997 ?
Yeah, I'll close this then.
 This is enough, no need to create an array:
 
 template EnumLength(E) if (is(E == enum))
 {
     enum EnumLength = __traits(allMembers, E).length;
 }
Thanks, I forgot tuples have length.
 But your problem is probably solved in another way, creating an "enum
 duplicator".
Well in my real code the "subtyped" enum has different field names but the same values as another enum. Anyway it's not an issue as long as I can get the length of the enum. *** This issue has been marked as a duplicate of issue 4997 *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 01 2012