Get symbols (and/or UDAs) of subclass from superclass

James Blachly james.blachly at gmail.com
Sun Mar 15 20:18:03 UTC 2020


I would like to programmatically retrieve members of a subclass to 
create a self-documenting interface. I am afraid that my approach is not 
possible due to need for compile time __traits / std.traits, and runtime 
typeinfo. My proposed approach is as follows:

class Base
{
     string whatever;

     string toString()
     {
         // loop over __traits(allMembers, typeof(this)) or 
getSymbolsByUDA, etc.
     }
}

/// There may be a dozen Derived classes
class Derived1 : Base
{
     @Config("a name", "a description")
     float probability;
}
class Derived2 : Base
{
     @Config("another", "more description")
     int replicates;
}
...

Unfortunately, I am afraid this doesn't look possible because of the 
need for compile-time UDAs and runtime TypeInfo. Is there a way to do 
this without re-implementing toString in every single derived class? I 
expect there to be many derived classes.

Other ideas welcomed, as I usually write C-style D and am only recently 
taking a stab at OO Inheritance features.

Thanks in advance.
James


More information about the Digitalmars-d-learn mailing list