I have a module:
module mymodule;
enum foo { one, three, seven};
static int[foo] mymap;
...
static this() {
mymap = [
one:1,
three:3,
seven:7
]
}
it works, but I want to get compile-time error if I add new value to foo
enum and don't add it to initialization. How can I do it in the right way?
P.S. I always can do (after AA initialization):
foreach(m; std.traits.EnumMembers!foo)
enforce(m in mymap, to!string(m) ~ " is absent!");
but I don't think it is elegant

Alexandr Druzhinin:
> but I don't think it is elegant
If you think that's not elegant, then try to do the opposite:
instead of giving the full mymap in the static this, try to
create it from the elements of the enum itself.
In D the enum name is better starting with an upper case, and the
end semicolon is not needed:
enum Foo { one, three, seven }
Bye,
bearophile

12.08.2012 18:52, bearophile пишет:
> Alexandr Druzhinin:
>
>> but I don't think it is elegant
>
> If you think that's not elegant, then try to do the opposite: instead of
> giving the full mymap in the static this, try to create it from the
> elements of the enum itself.
>
> In D the enum name is better starting with an upper case, and the end
> semicolon is not needed:
>
> enum Foo { one, three, seven }
>
> Bye,
> bearophile
I can't create mymap automatically based on Foo elements, because
there's no correlation between them - I should just set this
corrrelation by means of mymap.
What about elegance - I just thought that there was some better way to
check if all elements of Foo were included into mymap than just brutal
force iteration through all Foo elements.

Alexandr Druzhinin:
> I just thought that there was some better way to check if all
> elements of Foo were included into mymap than just brutal force
> iteration through all Foo elements.
There are many ways, like (untested):
AA.keys.sort().equals([EnumMembers!MyEnum].sort())
Bye,
bearophile

On Sunday, 12 August 2012 at 11:36:26 UTC, Alexandr Druzhinin
wrote:
> it works, but I want to get compile-time error if I add new
> value to foo enum and don't add it to initialization. How can I
> do it in the right way?
Use a final switch statement.
No, seriously, that's the only built-in facility that comes to my
mind which checks for the presence of all enum members.
Otherwise, you have to put an assert in manually – which, in my
opinion, is not exactly a problem either.
David