Getting object members

Adam D. Ruppe destructionator at gmail.com
Wed Jun 12 08:12:31 PDT 2013


On Wednesday, 12 June 2013 at 10:59:42 UTC, Szymon Gatner wrote:
> I know how to iterate over members when type is known at 
> compile time (with __traits) but I can't find a documentation 
> of how to get them polymorphically, I mean:

It hasn't been implemented in the runtime yet (though all the 
pieces are there), so unless each class makes the info available 
itself (or you want to modify your runtime) you can't.

For example:

claas Foo {
    int x, y;
    mixin ReflectionInfo!Foo;
}


Where ReflectionInfo is something you'd write. Jacob Carlborg has 
written a serialization library that works like this: 
https://github.com/jacob-carlborg/orange

But even so it wouldn't work through Object or classinfo unless 
you modify druntime. I'd like to see some functionality for this 
in for the next release (or maybe the one after it) though.

> Also: Is there any article / tutorial on D's introspection 
> capabilities. I heard it is pretty powerful and I would really 
> like to try it.

I don't think so. The basic idea though is:

foreach(member; __traits(allMembers, SomeClass)) {
    // member is a string, the name of the member
    // get the thing like this:
    __traits(getMember, SomeClass, member)
}


Then you look into it with the other traits and use std.traits 
from the stdlib for helper functions.


More information about the Digitalmars-d-learn mailing list