www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6617] New: Two problems using enum lenghs

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

           Summary: Two problems using enum lenghs
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This is a bug report. I have found two different problems while creating a
fixed-sized array as long as the number members of an enum (DMD 2.055beta3):


import std.traits: EnumMembers;
enum Foo : size_t { A = 0, B = 1, C = 2 }
void main() {
    int[(EnumMembers!(Foo)).length] bar1; // Error: EnumMembers!(Foo) is used
as a type
    enum size_t nmembers0 = (EnumMembers!(Foo)).length; // Error:
EnumMembers!(Foo) is used as a type
    alias EnumMembers!(Foo) members;
    int[members.length] bar2; // Error: identifier 'length' of 'members.length'
is not defined
    enum size_t nmembers = members.length;
    int[nmembers] bar3; // OK
}


---------------

In practice I think a length attribute for enums is handy to have (this is a
low-priority enhacement request):


enum Foo : size_t { A = 0, B = 1, C = 2 }
void main() {
    int[Foo.length] bar;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 07 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6617


Tommi <tommitissari hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tommitissari hotmail.com




 In practice I think a length attribute for enums is handy to have (this is a
 low-priority enhacement request):
I think it's a good enhancement, but instead of "length" it should be called "count". Tool {hammer, drill, screwdriver} Tool.count; // a lot better than Tool.length -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6617


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |
                 CC|                            |andrej.mitrovich gmail.com
           Severity|normal                      |enhancement



10:06:34 PST ---

 This is a bug report. I have found two different problems while creating a
 fixed-sized array as long as the number members of an enum (DMD 2.055beta3)
All of these now work.
 In practice I think a length attribute for enums is handy to have (this is a
 low-priority enhacement request):
 enum Foo : size_t { A = 0, B = 1, C = 2 }
 void main() {
     int[Foo.length] bar;
 }
The problem with this is that lookups also go to the base type of the enum, so: struct T { property static size_t length() { return 0; } } enum Foo : T { a = T() } void main() { assert(Foo.length == 1); // fails } We could instead provide a template in std.traits: template EnumLength(E) if (is(E == enum)) { enum size_t EnumLength = EnumMembers!E.length; } void main() { assert(EnumLength!Foo == 1); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6617


bearophile_hugs eml.cc changed:

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





 This is a bug report. I have found two different problems while creating a
 fixed-sized array as long as the number members of an enum (DMD 2.055beta3)
All of these now work.
Right, I close this bug report. Issue 4997 covers the enhancement request.
 The problem with this is that lookups also go to the base type of the enum, so:
 
 struct T
 {
      property static size_t length() { return 0; }
 }
 
 enum Foo : T { a = T() }
 
 void main()
 {
     assert(Foo.length == 1);  // fails
 }
Enums already have some built-in attributes, that cause some troubles. In Issue 4997 I have suggested a namespace named "meta", so you write "MyEnum.meta.length". But then a problem is that MyEnum.meta.max breaks the convention that D integral types have a "max" attribute. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6617






 We could instead provide a template in std.traits:
 
 template EnumLength(E) if (is(E == enum))
 {
     enum size_t EnumLength = EnumMembers!E.length;
 }
 
 void main()
 {
     assert(EnumLength!Foo == 1);
 }
I think this gives too much little improvement :-) Such EnumLength template is not needed in Phobos. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 02 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6617




10:52:48 PST ---

 Enums already have some built-in attributes, that cause some troubles. In 
 Issue 4997 I have suggested a namespace named "meta", so you write
 "MyEnum.meta.length".
This is also implementable via a template that exposes some fields that you need. There's no need to hack around the compiler for this. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 02 2012