Thread overview
[Issue 6617] New: Two problems using enum lenghs
Jul 16, 2012
Tommi
Dec 02, 2012
Andrej Mitrovic
Dec 02, 2012
Andrej Mitrovic
September 07, 2011
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


--- Comment #0 from bearophile_hugs@eml.cc 2011-09-07 02:10:03 PDT ---
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: -------
July 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6617


Tommi <tommitissari@hotmail.com> changed:

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


--- Comment #1 from Tommi <tommitissari@hotmail.com> 2012-07-16 10:14:48 PDT ---
(In reply to comment #0)
> 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: -------
December 02, 2012
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


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-02 10:06:34 PST ---
(In reply to comment #0)
> 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: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6617


bearophile_hugs@eml.cc changed:

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


--- Comment #3 from bearophile_hugs@eml.cc 2012-12-02 10:32:59 PST ---
(In reply to comment #2)
> (In reply to comment #0)
> > 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: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6617



--- Comment #4 from bearophile_hugs@eml.cc 2012-12-02 10:34:49 PST ---
(In reply to comment #2)

> 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: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6617



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-02 10:52:48 PST ---
(In reply to comment #3)
> 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: -------