Tuple enumeration without integers or strings

Rekel paultjeadriaanse at gmail.com
Fri Jan 1 23:14:43 UTC 2021


I seem to have hit a bit of a wall when comparing D enums to Java 
enums.
Of course the latter is just a fancy class, though it'd be nice 
to see partially equivalent usefulness regardless.

For example, as soon as non-integer, non-string values are given 
to enums things get messy for me when using switch cases, I 
haven't yet found a satisfying way of doing this.
Note the reason I'm using tuples is to somehow replicate the way 
java enumerals can contain several member variables, which tends 
to be very useful.

Something along the lines of the following is what i'd like to 
achieve;
> alias Direction = Tuple!(byte, "x", byte, "y");
> enum Wind {N = Direction(0, 1) ... etc}
> ...
> void some_function(Wind w) {
>     switch (w) {
>         case Wind.N:
>             ... etc
>             break;
>         ... etc
>         default:
>             assert(0);
>     }
> }

One thing that might have worked would have been an equivalent of 
java's "ordinal", though from what I've found D doesn't seem to 
have something equivalent to this?
(I'd prefer not to have a seperate tuple member purely for 
ordinality, and dropping enumerals altogether also seems like a 
waste)


More information about the Digitalmars-d-learn mailing list