www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting member by name in runtime

reply Marek Janukowicz <marek janukowicz.net> writes:
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
Aug 21 2013
next sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Aug 21 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Aug 21 2013
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Aug 21 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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;
Aug 21 2013
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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"...
Aug 21 2013