std.reflection prototype

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 17 08:19:46 PDT 2015


On Fri, 17 Apr 2015 05:30:04 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2015-04-17 01:25, bitwise wrote:
>
>> Ok, that sounds right. D has no multiple or virtual inheritance, so I
>> guess things should be fine. C# is the same way(single inheritance,
>> interfaces) which is likely designed to avoid these kinds of issues.
>>
>> I would be modifying the offTi() property though, not getMembers().
>> getMembers() actually works right now, although it's kinda useless.
>> Basically, if you put any function named "getMembers" at module scope,
>> the address of that function can be retrieved using
>> ModuleInfo.xgetMembers().
>
> We need a "getMembers" on ClassInfo. Because "typeid" will resolve the  
> dynamic type. The problem is this code:
>
> class Bar {}
> class Foo : Bar {}
>
> void main ()
> {
>      Bar a = new Foo;
>      writeln(typeid(a));
> }
>
> Compile time reflection cannot be used to get the members in this case.
>


Sorry, that's what I meant(using this on classes, not modules).  
xgetMembers/getMembers seems to be designed so that the caller can write  
their own getMembers() function, whereas offTi() is designed to return an  
array that was automatically generated.

offTi() is actually defined in TypeInfo:
https://github.com/D-Programming-Language/druntime/blob/cfcf7480b2faea0af9ab6ddba8e3b0d9f05c4415/src/object_.d#L301

and overridden for TypeInfo_Class:
https://github.com/D-Programming-Language/druntime/blob/cfcf7480b2faea0af9ab6ddba8e3b0d9f05c4415/src/object_.d#L831

but TypeInfo_Class.m_offTi is never populated:
https://github.com/D-Programming-Language/dmd/blob/master/src/toobj.c#L436

So while this code technically works, there will be no output:

class Test {
     int a = 4;
     int b = 5;
     int c = 6;
}

void main()
{
     foreach(const(OffsetTypeInfo) off; typeid(Test).offTi())
         writeln(off.offset);
}


More information about the Digitalmars-d mailing list