How can I use structs in a named enum?

2012-02-29 Thread simendsjo

.. That is  enum E : Struct {}

struct S {
int a;
}

// ok
enum E : S {
a = S(1),
}

// Error: need member function opCmp() for struct S to compare
/*
enum E2 : S {
a = S(1),
b = S(2)
}
*/

struct S2 {
int a;
int opCmp(ref const S2 other)
{
return a == other.a ? 0 : (a  other.a ? -1 : 1);
}
}

// Error: function opcmp.S2.opCmp (ref const(S2) other) is not callable  
using argument types (E3)

// Error: cast(const(S2))S2(1) is not an lvalue
// Error: function opcmp.S2.opCmp (ref const(S2) other) is not callable  
using argument types (E3)

// Error: cast(const(S2))S2(1) is not an lvalue
enum E3 : S2 {
a = S2(1),
b = S2(2)
}

void main() {
}


So E3 is passed as the parameter to S2.opCmp..?


Re: How can I use structs in a named enum?

2012-02-29 Thread simendsjo

On Wed, 29 Feb 2012 12:27:32 +0100, simendsjo simend...@gmail.com wrote:

On Wed, 29 Feb 2012 12:15:35 +0100, simendsjo simend...@gmail.com  
wrote:



.. That is  enum E : Struct {}

struct S {
 int a;
}

// ok
enum E : S {
 a = S(1),
}

// Error: need member function opCmp() for struct S to compare
/*
enum E2 : S {
 a = S(1),
 b = S(2)
}
*/

struct S2 {
 int a;
 int opCmp(ref const S2 other)
 {
 return a == other.a ? 0 : (a  other.a ? -1 : 1);
 }
}

// Error: function opcmp.S2.opCmp (ref const(S2) other) is not callable  
using argument types (E3)

// Error: cast(const(S2))S2(1) is not an lvalue
// Error: function opcmp.S2.opCmp (ref const(S2) other) is not callable  
using argument types (E3)

// Error: cast(const(S2))S2(1) is not an lvalue
enum E3 : S2 {
 a = S2(1),
 b = S2(2)
}

void main() {
}


So E3 is passed as the parameter to S2.opCmp..?


Hmm.. By removing ref const in opCmp, it works as expected


Btw.. Removing the ref is obvious, but why do I have to remove const?
Perhaps also the documentation for opCmp should be improved..?


Re: How can I use structs in a named enum?

2012-02-29 Thread bearophile
simendsjo:

 Perhaps also the documentation for opCmp should be improved..?

Also, and maybe here DMD has to give better error messages :-)

Bye,
bearophile


Re: How can I use structs in a named enum?

2012-02-29 Thread simendsjo
On Wed, 29 Feb 2012 13:40:51 +0100, bearophile bearophileh...@lycos.com  
wrote:



simendsjo:


Perhaps also the documentation for opCmp should be improved..?


Also, and maybe here DMD has to give better error messages :-)

Bye,
bearophile


Yes. Using argument types E3 doesn't make much sense when the named enum  
is a complex type.


Re: How can I use structs in a named enum?

2012-02-29 Thread bearophile
simendsjo:

 Yes. Using argument types E3 doesn't make much sense when the named enum  
 is a complex type.

If this diagnostic bug/enhancement is not in Bugzilla then I suggest you to add 
it. Now there are two persons that are quite efficient at fixing Bugzilla bugs, 
but they need to know there is a problem.

Bye,
bearophile


Re: How can I use structs in a named enum?

2012-02-29 Thread Jonathan M Davis
On Wednesday, February 29, 2012 12:15:35 simendsjo wrote:
 .. That is  enum E : Struct {}
 
 struct S {
 int a;
 }
 
 // ok
 enum E : S {
 a = S(1),
 }
 
 // Error: need member function opCmp() for struct S to compare
 /*
 enum E2 : S {
 a = S(1),
 b = S(2)
 }
 */
 
 struct S2 {
 int a;
 int opCmp(ref const S2 other)
 {
 return a == other.a ? 0 : (a  other.a ? -1 : 1);
 }
 }
 
 // Error: function opcmp.S2.opCmp (ref const(S2) other) is not callable
 using argument types (E3)
 // Error: cast(const(S2))S2(1) is not an lvalue
 // Error: function opcmp.S2.opCmp (ref const(S2) other) is not callable
 using argument types (E3)
 // Error: cast(const(S2))S2(1) is not an lvalue
 enum E3 : S2 {
 a = S2(1),
 b = S2(2)
 }
 
 void main() {
 }
 
 
 So E3 is passed as the parameter to S2.opCmp..?

http://d.puremagic.com/issues/show_bug.cgi?id=4423

- Jonathan M Davis


Re: How can I use structs in a named enum?

2012-02-29 Thread simendsjo
On Wed, 29 Feb 2012 19:00:10 +0100, bearophile bearophileh...@lycos.com  
wrote:



simendsjo:

Yes. Using argument types E3 doesn't make much sense when the named  
enum

is a complex type.


If this diagnostic bug/enhancement is not in Bugzilla then I suggest you  
to add it. Now there are two persons that are quite efficient at fixing  
Bugzilla bugs, but they need to know there is a problem.


Bye,
bearophile


http://d.puremagic.com/issues/show_bug.cgi?id=7612