Thread overview
complete initialization of the static allocated array
Aug 12, 2012
Alexandr Druzhinin
Aug 12, 2012
bearophile
Aug 12, 2012
Alexandr Druzhinin
Aug 12, 2012
bearophile
Aug 12, 2012
bearophile
Aug 12, 2012
Alexandr Druzhinin
Aug 12, 2012
bearophile
Aug 12, 2012
David Nadlinger
August 12, 2012
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
August 12, 2012
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
August 12, 2012
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.

August 12, 2012
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
August 12, 2012
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
August 12, 2012
> There are many ways, like (untested):
>
> AA.keys.sort().equals([EnumMembers!MyEnum].sort())

Seems to work:

import std.algorithm, std.traits;

enum Foo { one, three, seven};
static int[Foo] myMap;

static this() {
    with (Foo)
        myMap = [
            one: 1,
            three: 3,
            seven: 7
        ];
    assert(myMap.keys.sort().equal([EnumMembers!Foo].sort()));
}

void main() {}

Bye,
bearophile
August 12, 2012
12.08.2012 19:19, bearophile пишет:
>
> There are many ways, like (untested):
>
> AA.keys.sort().equals([EnumMembers!MyEnum].sort())
>
> Bye,
> bearophile

i've tested - it works fine:

import std.algorithm;	// for equal()
import std.traits;	// for EnumMembers(T)

...

enforce(equal(AA.keys.sort, [EnumMembers!MyEnum].sort), "Assert message");


I learn D and I'm interested in the ways specific to D like yours example above to get power of D. Thanks!
August 12, 2012
Alexandr Druzhinin:

> import std.algorithm;	// for equal()
> import std.traits;	// for EnumMembers(T)

The right way to write that in D is:

import std.algorithm: equal;
import std.traits: EnumMembers;

Bye,
bearophile