Class Data Members Name Reflection

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 10 09:43:34 PDT 2014


On Tuesday, 10 June 2014 at 16:30:52 UTC, Nordlöw wrote:
> What trait should I use to filter out data members?

No trait, more like an is thing to see if the thing has an init. 
I think

static if(is(typeof(__traits(getMember, Thing, name).init)) { }

will do it. (BTW the sample chapter of my book talks about 
reflection, I think I talked about some of this in there: 
http://www.packtpub.com/discover-advantages-of-programming-in-d-cookbook/book 
)

> foreach (ix, memb; arg.args[0].front.tupleof)

Eeek, I actually used s for a reason there - it gives you a 
simple name that is easily repeated and filtered. The nasty 
string you're seeing is the name of a compiler-generated 
temporary variable in the foreach.

That said, your field names are in there at the end, so another 
option would be to run it through lastIndexOf(".") and then slice 
to that.

So it works backward to the dot and slices off the rest of the 
string.

string s = arg.args[0].front.tupleof[idx].stringof;
auto dotIndex = s.lastIndexOf(".");
assert(dotIndex >= 0); // it ought to be there anyway
auto name = s[dotIndex + 1 .. $]; // slice off the temp name, 
whatever it is



More information about the Digitalmars-d-learn mailing list