Jump to page: 1 2
Thread overview
getting Key:Value pairs of an enum at compile time ?
Jan 23, 2014
Uplink_Coder
Jan 23, 2014
Jacob Carlborg
Jan 23, 2014
Uplink_Coder
Jan 23, 2014
Stanislav Blinov
Jan 23, 2014
Uplink_Coder
Jan 23, 2014
Stanislav Blinov
Jan 23, 2014
Uplink_Coder
Jan 23, 2014
Uplink_Coder
Jan 23, 2014
Stanislav Blinov
Jan 23, 2014
Uplink_Coder
Jan 23, 2014
Rikki Cattermole
Jan 23, 2014
Meta
January 23, 2014
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 ?

January 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
January 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 {.
January 23, 2014
> Try getMember: http://dlang.org/traits.html#getMember
if i try it, I get __aggr2297 cannot be read at compile time
January 23, 2014
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);
    }
}
January 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
January 23, 2014
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?
January 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();
}
January 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;
}

January 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;
}
« First   ‹ Prev
1 2