www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overloading operations for enums

reply "Dominikus Dittes Scherkl" writes:
Hello.

I want to create some finite algebra, where the elements are 
enumerated but operations on them are defined (with composition 
tables).

e.g.:

enum color = { white, yellow, red, blue, orange, violet, green, 
black };

color a = blue;
a += yellow;
assert(a == green);

is this possible in D?

Because, if I define a struct, I can define operation overloads, 
but then I have no enumeration of possible values. But if I 
enumerate, I cannot
overload operations.
What have I missed?
May 26 2014
parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Monday, 26 May 2014 at 16:54:02 UTC, Dominikus Dittes Scherkl 
wrote:
 Hello.

 I want to create some finite algebra, where the elements are 
 enumerated but operations on them are defined (with composition 
 tables).

 e.g.:

 enum color = { white, yellow, red, blue, orange, violet, green, 
 black };

 color a = blue;
 a += yellow;
 assert(a == green);

 is this possible in D?

 Because, if I define a struct, I can define operation 
 overloads, but then I have no enumeration of possible values. 
 But if I enumerate, I cannot
 overload operations.
 What have I missed?
You can make an enumeration of structs: struct S { int i; } enum E { Something = S(1), SomethingElse = S(2) }
May 26 2014