Class methods in D?

Steven Schveighoffer schveiguy at yahoo.com
Thu May 3 12:17:08 PDT 2012


On Thu, 03 May 2012 14:05:26 -0400, Mehrdad <wfunction at hotmail.com> wrote:

>> There's the RTInfo method I told you about (recently added) if you want  
>> to stick the information directly into TypeInfo at compile time.
>>
>> There's also static ctors.  Just add a hashtable based on the class  
>> name, and use typeid(this).name as the initial key.  You have to handle  
>> all the inheritance of properties yourself, but that shouldn't be too  
>> difficult.
>>
>> -Steve
>
> Ooh... how do I get RTInfo? It's not in druntime/phobos....

https://github.com/D-Programming-Language/druntime/blob/master/import/object.di#L622

Need the github versions of dmd/druntime/phobos.

Basically how it works is the compiler evaluates RTInfo!T where T is the  
type for which TypeInfo is being generated, and places the result into a  
location that is returned by TypeInfo.rtInfo property.  This all happens  
at compile-time, so it effectively gives you a hook to store runtime type  
information into the TypeInfo.

As far as I know, there's not official documentation on it, it's a pretty  
beta feature.  But I think it will open a door to actual runtime type  
information that we never had before.

One limitation is, it has to be inside the runtime, and specifically in  
object.di.  However, we may be able to effectively generate class/struct  
specific runtime type information.  For example:

struct RTTI
{
   // druntime-specified properties
   ...
   // type-specific properties
   void *typeSpecificProperties;
}

template RTInfo_Instance(T)
{
    static if(is(typeof({void *x = T.__rtInfo();}))
      immutable RTTI RTInfo_Instance = RTTI(..., T.__rtInfo());
    else
      immutable RTTI RTInfo_Instance = RTTI(..., null);
}

template RTInfo(T)
{
    immutable void * RTInfo = &RTInfo_Instance!T;
}

I have no idea if this works :)  But I'm *positive* someone with better  
template skills than me can make something like this work.

-Steve


More information about the Digitalmars-d mailing list