Thread overview
Getting member by name in runtime
Aug 21, 2013
Marek Janukowicz
Aug 21, 2013
Jacob Carlborg
Aug 21, 2013
Adam D. Ruppe
Aug 21, 2013
John Colvin
Aug 21, 2013
John Colvin
Aug 21, 2013
H. S. Teoh
August 21, 2013
I get a string from HTTP request and I need to process a member of this name of an object. I would like to use __traits(getMember) for that, but of course it doesn't work with runtime parameters. So what I ended up with is:

foreach( s; __traits(derivedMembers, typeof(obj))) {
  if (s == name) ...
}

Of course this looks suboptimal as I run the loop until I find a member matching the name. Is there any better way to do this?

-- 
Marek Janukowicz
August 21, 2013
On 2013-08-21 10:10, Marek Janukowicz wrote:
> I get a string from HTTP request and I need to process a member of this name
> of an object. I would like to use __traits(getMember) for that, but of
> course it doesn't work with runtime parameters. So what I ended up with is:
>
> foreach( s; __traits(derivedMembers, typeof(obj))) {
>    if (s == name) ...
> }
>
> Of course this looks suboptimal as I run the loop until I find a member
> matching the name. Is there any better way to do this?

I don't think so. Unless you build a set or associative array at compile time with all the members of a given type.

-- 
/Jacob Carlborg
August 21, 2013
On Wednesday, 21 August 2013 at 12:03:38 UTC, Jacob Carlborg wrote:
> Unless you build a set or associative array at compile time with all the members of a given type.

Or build a string with a switch() { case "member1": member1(); break; case "mem2": mem2(); }

then mix that in and hope the compiler optimizes the switch.
August 21, 2013
On Wednesday, 21 August 2013 at 08:08:56 UTC, Marek Janukowicz wrote:
> I get a string from HTTP request and I need to process a member of this name
> of an object. I would like to use __traits(getMember) for that, but of
> course it doesn't work with runtime parameters. So what I ended up with is:
>
> foreach( s; __traits(derivedMembers, typeof(obj))) {
>   if (s == name) ...
> }
>
> Of course this looks suboptimal as I run the loop until I find a member
> matching the name. Is there any better way to do this?

I wrote something for this a while back for loading config files:

https://github.com/John-Colvin/ES/blob/master/JCutils.d#L195

You'll need everything from that line downwards.
August 21, 2013
On Wednesday, 21 August 2013 at 13:00:40 UTC, John Colvin wrote:
> On Wednesday, 21 August 2013 at 08:08:56 UTC, Marek Janukowicz wrote:
>> I get a string from HTTP request and I need to process a member of this name
>> of an object. I would like to use __traits(getMember) for that, but of
>> course it doesn't work with runtime parameters. So what I ended up with is:
>>
>> foreach( s; __traits(derivedMembers, typeof(obj))) {
>>  if (s == name) ...
>> }
>>
>> Of course this looks suboptimal as I run the loop until I find a member
>> matching the name. Is there any better way to do this?
>
> I wrote something for this a while back for loading config files:
>
> https://github.com/John-Colvin/ES/blob/master/JCutils.d#L195
>
> You'll need everything from that line downwards.

struct A
{
    int a,b,c,d,e,f,g,h,i,j,k;
}

A a;
auto byString = string_access!int(a);

string name = //some RT val
int val = //some RT val

byString[name] = val;
August 21, 2013
On Wed, Aug 21, 2013 at 10:10:29AM +0200, Marek Janukowicz wrote:
> I get a string from HTTP request and I need to process a member of this name of an object. I would like to use __traits(getMember) for that, but of course it doesn't work with runtime parameters. So what I ended up with is:
> 
> foreach( s; __traits(derivedMembers, typeof(obj))) {
>   if (s == name) ...
> }
> 
> Of course this looks suboptimal as I run the loop until I find a member matching the name. Is there any better way to do this?
[...]

Maybe you could put this foreach inside a static this() (i.e. module
ctor) and have it initialize a hash table of members at startup time?
Then you can use the hash to look things up quickly.


T

-- 
I'm still trying to find a pun for "punishment"...