Thread overview
[Issue 4423] New: enums of struct types
Jul 04, 2010
Jonathan M Davis
[Issue 4423] TDPL enums of struct types
Nov 29, 2010
Jonathan M Davis
Jun 20, 2011
Jonathan M Davis
[Issue 4423] [CTFE] TDPL enums of struct types
Jun 29, 2011
Don
July 04, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4423

           Summary: enums of struct types
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jmdavisProg@gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg@gmail.com> 2010-07-04 03:23:53 PDT ---
I would like to be able to create enums of struct types, but it doesn't seem possible at the moment. You can create manifest constants of a struct using enum, but not an enum with an actual list of values. So, if I were to create a struct that looks like this

struct S
{
    this(string phrase, int num)
    {
        this.phrase = phrase;
        this.num = num;
    }

    int opCmp(const ref S rhs)
    {
        if(phrase < rhs.phrase)
            return -1;
        else if(phrase > rhs.phrase)
            return 1;

        if(num < rhs.num)
            return -1;
        else if(num > rhs.num)
            return 1;

        return 0;
    }

    string phrase;
    int    num;
}


I could create enums like this

enum a = S("hello", 1);
enum b = S("goodbye", 45);


and the compiler is fine with it. However, if I try and create a bona fide enum

enum E : S {a = S("hello", 1),
            b = S("goodbye", 45),
            c = S("world", 22)};


the compiler complains like so

t.d(28): Error: variable __ctmp1 cannot be read at compile time
t.d(28): Error: cannot evaluate __ctmp1.this("hello",1) at compile time
t.d(28): Error: cannot evaluate __ctmp1.this("hello",1) at compile time
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(29): Error: cannot evaluate
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) at
compile time
t.d(29): Error: Integer constant expression expected instead of
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) < 0
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(29): Error: cannot evaluate
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) at
compile time
t.d(29): Error: Integer constant expression expected instead of
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) > 0
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(30): Error: cannot evaluate
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) at
compile time
t.d(30): Error: Integer constant expression expected instead of
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) < 0
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(30): Error: cannot evaluate
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) at
compile time
t.d(30): Error: Integer constant expression expected instead of
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) > 0


It doesn't look like the CTFE stuff can handle this for some reason. Granted, what CTFE can do is a definite work in progress, but I think that the lack of ability to create an enum of structs is seriously limiting and rather odd given that you can create a manifest constant of a struct using an enum. So, I'd love it if it were possible to create actual enums of a struct type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 29, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4423


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|enums of struct types       |TDPL enums of struct types
         OS/Version|Linux                       |All
           Severity|enhancement                 |normal


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-28 20:48:56 PST ---
I'm elevating this to a bug, since TDPL clearly says that you can have enums of struct types.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4423


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 20, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4423


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid


--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-06-19 20:09:48 PDT ---
Why is this assigned to Andrei of all people? As far as I know, he never does any work on the compiler.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 20, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4423


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|andrei@metalanguage.com     |nobody@puremagic.com


--- Comment #3 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-06-20 06:02:50 PDT ---
Resetting asignee to default.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4423


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |6221


--- Comment #4 from Don <clugdbug@yahoo.com.au> 2011-06-29 01:38:57 PDT ---
This works in git master, if opCmp has signature
int opCmp(S rhs)            instead of
int opCmp(const ref S rhs).

The problem lies in enum.c, which tries to determine max and min for the enum
by comparing the each element in the enum. Since they are struct literals, they
aren't lvalues, so 'const ref' fails.
This is an example of bug 6221.

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