Getting member by name in runtime

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Aug 21 07:48:28 PDT 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"...


More information about the Digitalmars-d-learn mailing list