www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - complete initialization of the static allocated array

reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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
Aug 12 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 12 2012
parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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.
Aug 12 2012
parent Alexandr Druzhinin <drug2004 bk.ru> writes:
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!
Aug 12 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 12 2012
prev sibling next sibling parent "David Nadlinger" <see klickverbot.at> writes:
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
Aug 12 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
 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
Aug 12 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 12 2012