digitalmars.D.learn - getting Key:Value pairs of an enum at compile time ?
- Uplink_Coder (13/13) Jan 23 2014 Hello,
- Jacob Carlborg (4/17) Jan 23 2014 Try getMember: http://dlang.org/traits.html#getMember
- Uplink_Coder (1/2) Jan 23 2014 if i try it, I get __aggr2297 cannot be read at compile time
- Stanislav Blinov (8/10) Jan 23 2014 There's a convenience wrapper for that, EnumMembers:
- Uplink_Coder (2/7) Jan 23 2014 Still not there at compile-time
- Stanislav Blinov (2/9) Jan 23 2014 Could you show an exact code that fails?
- Uplink_Coder (13/14) Jan 23 2014 sure!
- Uplink_Coder (12/12) Jan 23 2014 Oh my bad
- Stanislav Blinov (19/20) Jan 23 2014 On Thursday, 23 January 2014 at 17:59:05 UTC, Uplink_Coder wrote:
- Uplink_Coder (2/2) Jan 23 2014 Great now it works!
- Rikki Cattermole (15/28) Jan 23 2014 import std.stdio;
- Meta (22/35) Jan 23 2014 import std.traits;
Hello, I have an Enum to represent possible Options for a selectList e.g enum options { blueish="Blue", redish ="Red" } and I want the List-Entrys to say "blueish" or "redish". the value send to the app should be "Blue" or "Red". I can use __traits(allMembers,Enum) for the identifiers, but how do I get the Values ?
Jan 23 2014
On 2014-01-23 16:07, Uplink_Coder wrote:Hello, I have an Enum to represent possible Options for a selectList e.g enum options { blueish="Blue", redish ="Red" } and I want the List-Entrys to say "blueish" or "redish". the value send to the app should be "Blue" or "Red". I can use __traits(allMembers,Enum) for the identifiers, but how do I get the Values ?Try getMember: http://dlang.org/traits.html#getMember -- /Jacob Carlborg
Jan 23 2014
Try getMember: http://dlang.org/traits.html#getMemberif i try it, I get __aggr2297 cannot be read at compile time
Jan 23 2014
On Thursday, 23 January 2014 at 15:31:53 UTC, Uplink_Coder wrote:There's a convenience wrapper for that, EnumMembers: void main() { foreach(m; EnumMembers!Options) { writeln(m, " ", cast(string)m); } }Try getMember: http://dlang.org/traits.html#getMemberif i try it, I get __aggr2297 cannot be read at compile time
Jan 23 2014
void main() { foreach(m; EnumMembers!Options) { writeln(m, " ", cast(string)m); } }Still not there at compile-time ... because I need to index my members
Jan 23 2014
On Thursday, 23 January 2014 at 17:15:41 UTC, Uplink_Coder wrote:Could you show an exact code that fails?void main() { foreach(m; EnumMembers!Options) { writeln(m, " ", cast(string)m); } }Still not there at compile-time ... because I need to index my members
Jan 23 2014
Could you show an exact code that fails?sure! auto EnumToSelect(Enum)(string Name = __traits(identifier,Enum)) if (is(Enum==enum)) { foreach (i,Item;[__traits(allMembers,Enum)]) { else form_string ~= "\toption(value='"~Item~"') "~__traits(getMember,Enum,Item)~"\n"; } debug import std.stdio; debug writeln(form_string); return form_string; }
Jan 23 2014
Oh my bad wasn't too carefull with deletion auto EnumToSelect(Enum)(string Name = __traits(identifier,Enum)) if (is(Enum==enum)) { foreach (i,Item;[__traits(allMembers,Enum)]) { reslt ~= "\toption(value='"~__traits(getMember,Enum,Item)~"') "~Item~"\n"; } debug import std.stdio; debug writeln(form_string); return reslt; }
Jan 23 2014
On Thursday, 23 January 2014 at 17:59:05 UTC, Uplink_Coder wrote: First and foremost, I'd suggest you use the beautiful solution proposed by Meta. But to illustrate problems with your code:foreach (i,Item;[__traits(allMembers,Enum)]) {This is the culprit. More specifically, you don't need to put the result of __traits into array. __traits(allMembers) returns a tuple which can be iterated at compile time. This is how it would look: auto EnumToSelect(Enum)(string Name = __traits(identifier,Enum)) if (is(Enum==enum)) { string form_string; // Note, there are no [] enclosing __traits foreach (i,Item;__traits(allMembers,Enum)) { form_string ~= "\toption(value='"~Item~"') " ~__traits(getMember,Enum,Item)~"\n"; } return form_string; }
Jan 23 2014
Great now it works! Thanks for your time :D
Jan 23 2014
On Thursday, 23 January 2014 at 15:07:18 UTC, Uplink_Coder wrote:Hello, I have an Enum to represent possible Options for a selectList e.g enum options { blueish="Blue", redish ="Red" } and I want the List-Entrys to say "blueish" or "redish". the value send to the app should be "Blue" or "Red". I can use __traits(allMembers,Enum) for the identifiers, but how do I get the Values ?import std.stdio; enum Options { blueish="Blue", redish ="Red" } void main() { foreach(m; __traits(allMembers, Options)) { writeln(m); writeln(cast(string)__traits(getMember, Options, m)); } } The cast forces it to grab the value. Note where Options values are of type string. Its the same as explicitly saying enum T : string {.
Jan 23 2014
On Thursday, 23 January 2014 at 15:07:18 UTC, Uplink_Coder wrote:Hello, I have an Enum to represent possible Options for a selectList e.g enum options { blueish="Blue", redish ="Red" } and I want the List-Entrys to say "blueish" or "redish". the value send to the app should be "Blue" or "Red". I can use __traits(allMembers,Enum) for the identifiers, but how do I get the Values ?import std.traits; import std.range; import std.conv; enum Nums { one, two, three, four, five, } void main() { //For a range of tuples auto kvRange = zip([EnumMembers!Nums], [EnumMembers!Nums] .to!(OriginalType!Nums[])) //For an associative array import std.array; auto aa = kvRange.assocArray(); }
Jan 23 2014