[Issue 4423] New: enums of struct types
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Jul 4 03:23:56 PDT 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 at puremagic.com
ReportedBy: jmdavisProg at gmail.com
--- Comment #0 from Jonathan M Davis <jmdavisProg at 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: -------
More information about the Digitalmars-d-bugs
mailing list