Runtime introspection, or how to get class members at runtime Fin D

Arafel er.krali at gmail.com
Wed Jun 6 14:15:06 UTC 2018


On 06/06/2018 03:52 PM, Adam D. Ruppe wrote:
> 
> It is possible to add it to the runtime library right now (there's a 
> thing called rtInfo in there made to hold it, it is just null right 
> now), just people fight over even a few bytes of added metadata. So if 
> it is added, it would surely be some opt-in thing that will require your 
> thing be recompiled anyway.

If I wanted to add it myself, would I need to create a personalised D 
compiler and/or D Runtime? That would be probably way too much for me :) 
Also, it would have to be distributed and used to create the plugins...

> 
> If you can recompile the library, you can add a parallel extended info 
> thing (MyReflectionInfo[TypeInfo] array perhaps, populated by a static 
> this() ctor created via compile time reflection) that gives what you need.
> 

Yeah, I had some ideas in this regard, still I would like it to be 
transparent from the plugin creator point of view, but I don't know if 
it would be possible to contain everything in the base class... so far I 
had though about a base class like this:

```
import std.traits;
import std.meta;

TypeInfo[string][TypeInfo_Class] RTInfo;

class Base {
     this(this C)() {
         if (typeid(C) in RTInfo)
             return;
         RTInfo[typeid(C)] = (TypeInfo[string]).init;
         static foreach_reverse(Type; AliasSeq!(C, BaseClassesTuple!C)) {
             static foreach(string field; FieldNameTuple!Type) {
                 RTInfo[typeid(Type)][field] = 
typeid(typeof(__traits(getMember, Type, field)));
             }
         }
     }
}

```

But I think children classes can bypass this constructor, so I guess 
it's not so easy, will have to keep trying :-)

A templated static this would be cool, though:

class Base {
     static this(this C)() {
         // ...
     }
}

Apparently it's not possible :-(


More information about the Digitalmars-d-learn mailing list