May 26, 2014 overloading operations for enums | ||||
---|---|---|---|---|
| ||||
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 Re: overloading operations for enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | 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)
}
|
Copyright © 1999-2021 by the D Language Foundation