digitalmars.D.learn - complete initialization of the static allocated array
- Alexandr Druzhinin (18/18) Aug 12 2012 I have a module:
- bearophile (9/10) Aug 12 2012 If you think that's not elegant, then try to do the opposite:
- Alexandr Druzhinin (7/17) Aug 12 2012 I can't create mymap automatically based on Foo elements, because
- bearophile (5/8) Aug 12 2012 There are many ways, like (untested):
- bearophile (16/18) Aug 12 2012 Seems to work:
- Alexandr Druzhinin (8/12) Aug 12 2012 i've tested - it works fine:
- bearophile (6/8) Aug 12 2012 The right way to write that in D is:
- David Nadlinger (8/11) Aug 12 2012 Use a final switch statement.
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
Alexandr Druzhinin:but I don't think it is elegantIf 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
12.08.2012 18:52, bearophile пишет:Alexandr Druzhinin: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.but I don't think it is elegantIf 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
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
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
12.08.2012 19:19, bearophile пишет:There are many ways, like (untested): AA.keys.sort().equals([EnumMembers!MyEnum].sort()) Bye, bearophilei'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
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
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