Challenge: Make a data type for holding one of 8 directions allowing increment and overflow
Salih Dincer
salihdb at hotmail.com
Wed Mar 13 20:26:03 UTC 2024
On Wednesday, 13 March 2024 at 10:27:49 UTC, Basile B. wrote:
> The semantics of the operators are actually not as clear as
> that. What if you define
>
> ```d
> enum Direction
> {
> N = 1, NE, S = 45, SW
> }
> ```
>
> ?
Certainly! EnumMembers; can be used. The EnumMembers template
from the std.traits module is used to retrieve all the members of
an enumeration. It generates a tuple containing all the
enumeration values, which can be iterated over using a foreach
loop. In the D programming language, you can use EnumMembers to
iterate over enum values at compile time, which is useful for
generating code based on enum members.
Here’s a simple code example:
```d
enum Days
{
Monday = 1001,
Tuesday = 1010,
Wednesday = 1011,
Thursday = 1100,
Friday = 1101,
Saturday = 1110,
Sunday = 1111
}
import std.traits : EnumMembers;
foreach(day; EnumMembers!Days)
day.writeln(":", cast(int)day);
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list