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

rikki cattermole rikki at cattermole.co.nz
Wed Jun 6 13:49:34 UTC 2018


On 07/06/2018 1:44 AM, Arafel wrote:
> On 06/06/2018 03:30 PM, rikki cattermole wrote:
>> You don't want TypeInfo.
> 
> Why not (genuine question)? There's even myObject.classinfo, and I can 
> only assume that there's some reason why it's there...
> 
>>
>>> In this case, what I'm trying to do is to serialize / dump / print 
>>> the contents of an object (class instance) without knowing its actual 
>>> runtime type.
>>>
>>> Before somebody suggests compile time introspection, the "main" code 
>>> where this routine lives only provides a base class, and it's up to 
>>> dlopen'ed plugins to provide the actual implementation... so I'm 
>>> sorry but no compile-time solution can possibly work.
>>>
>>> Also, having each derivative class provide their own dumping 
>>> information is not practical, I'd rather have it automated.
>>>
>>> I know it might not be the most idiomatic D, but as somebody with 
>>> mostly a Java background (with some C and just a bit of C++) it seems 
>>> something really straightforward to me: 
>>> myObject.getClass().getFields() [2].
>>
>> Doesn't exist.
>>
>>
> 
> Well, thanks for the quick and succinct answer... I guess the question 
> now would be how realistic it would be to propose such an addition to 
> the language... Has it already been discussed? (I tried searching the 
> forum, but didn't find anything relevant)
> 
> I know it's got a runtime penalty, but realistically speaking, spending 
> some bytes for the field names in the TypeInfo of a class shouldn't be 
> that much of a problem?

It is not an easy task building a reflection API from scratch. I'm one 
of the many that have tried. There is also push back from those who 
consider it "bloat" and don't need it.

You can't just extend TypeInfo, it was never designed for it.


More information about the Digitalmars-d-learn mailing list