iterate over enum name:value pairs
bearophile
bearophileHUGS at lycos.com
Sun Dec 8 17:56:23 PST 2013
Jay Norwood:
Using enums, despite their problems, if often better than strings.
> static Suits[] suits = [
> {Suit.spades, 1, 6, SuitShort.spd},
> {Suit.hearts, 4, 10, SuitShort.hrt},
> {Suit.diamonds, 4, 10, SuitShort.dmd},
> {Suit.clubs, 10, 16, SuitShort.clb}
> ];
>
> foreach (immutable member; suits)
In some cases you can also use with() to avoid struct/enum name
repetitions (unfortunately it creates a scope, so if you define
an immutable variable inside it, it will be invisible and
destroyed once the with() scope ends. This reduces the usefulness
of with()).
with (Suit) with (SuitShort)
{
static Suits[] suits = [
{spades, 1, 6, spd},
{hearts, 4, 10, hrt},
{diamonds, 4, 10, dmd},
{clubs, 10, 16, clb}
];
foreach (immutable member; suits)
...
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list