digitalmars.D.learn - Getting member by name in runtime
- Marek Janukowicz (10/10) Aug 21 2013 I get a string from HTTP request and I need to process a member of this ...
- Jacob Carlborg (5/13) Aug 21 2013 I don't think so. Unless you build a set or associative array at compile...
- Adam D. Ruppe (5/7) Aug 21 2013 Or build a string with a switch() { case "member1": member1();
- John Colvin (5/17) Aug 21 2013 I wrote something for this a while back for loading config files:
- John Colvin (10/30) Aug 21 2013 struct A
- H. S. Teoh (8/19) Aug 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
Aug 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
Aug 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.
Aug 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.
Aug 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: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;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
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