www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getting Key:Value pairs of an enum at compile time ?

reply "Uplink_Coder" <someemail someprovider.some> writes:
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
next sibling parent reply Jacob Carlborg <doob me.com> writes:
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
parent reply "Uplink_Coder" <someemail someprovider.some> writes:
 Try getMember: http://dlang.org/traits.html#getMember
if i try it, I get __aggr2297 cannot be read at compile time
Jan 23 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Thursday, 23 January 2014 at 15:31:53 UTC, Uplink_Coder wrote:
 Try getMember: http://dlang.org/traits.html#getMember
if i try it, I get __aggr2297 cannot be read at compile time
There's a convenience wrapper for that, EnumMembers: http://dlang.org/phobos/std_traits.html#.EnumMembers void main() { foreach(m; EnumMembers!Options) { writeln(m, " ", cast(string)m); } }
Jan 23 2014
parent reply "Uplink_Coder" <someemail someprovider.some> writes:
 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
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Thursday, 23 January 2014 at 17:15:41 UTC, Uplink_Coder wrote:
 void main() {
    foreach(m; EnumMembers!Options) {
        writeln(m, " ", cast(string)m);
    }
 }
Still not there at compile-time ... because I need to index my members
Could you show an exact code that fails?
Jan 23 2014
parent reply "Uplink_Coder" <someemail someprovider.some> writes:
 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
parent reply "Uplink_Coder" <someemail someprovider.some> writes:
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
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
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
parent "Uplink_Coder" <someemail someprovider.some> writes:
Great now it works!

Thanks for your time :D
Jan 23 2014
prev sibling next sibling parent "Rikki Cattermole" <alphaglosined gmail.com> writes:
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
prev sibling parent "Meta" <jared771 gmail.com> writes:
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