www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I use structs in a named enum?

reply simendsjo <simendsjo gmail.com> writes:
.. 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..?
Feb 29 2012
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On Wed, 29 Feb 2012 12:15:35 +0100, simendsjo <simendsjo 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
Feb 29 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Wed, 29 Feb 2012 12:27:32 +0100, simendsjo <simendsjo gmail.com> wrote:

 On Wed, 29 Feb 2012 12:15:35 +0100, simendsjo <simendsjo 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..?
Feb 29 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
simendsjo:

 Perhaps also the documentation for opCmp should be improved..?
Also, and maybe here DMD has to give better error messages :-) Bye, bearophile
Feb 29 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Wed, 29 Feb 2012 13:40:51 +0100, bearophile <bearophileHUGS 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.
Feb 29 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Feb 29 2012
parent simendsjo <simendsjo gmail.com> writes:
On Wed, 29 Feb 2012 19:00:10 +0100, bearophile <bearophileHUGS 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
Feb 29 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Feb 29 2012