Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 21, 2013 Getting member by name in runtime | ||||
---|---|---|---|---|
| ||||
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 Re: Getting member by name in runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marek Janukowicz | 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 Re: Getting member by name in runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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 Re: Getting member by name in runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marek Janukowicz | 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 Re: Getting member by name in runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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 Re: Getting member by name in runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marek Janukowicz | 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"... |
Copyright © 1999-2021 by the D Language Foundation