std.reflection prototype

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 31 01:51:45 PDT 2015


On 2015-03-30 23:31, bitwise wrote:
> https://github.com/D-Programming-Language/druntime/pull/775
> https://github.com/D-Programming-Language/dmd/pull/2271
>
> I've read over the comments of the above two pull-requests, and I'm
> confused about what is trying to be achieved.

Currently in object.d in druntime there's a template, RTInfo, declared 
something like this:

template RTInfo (T)
{
     enum RTInfo = null;
}

The compiler will automatically instantiate this template for all user 
defined types. What the template evaluates to will be placed in the 
TypeInfo for that given type in the "rtInfo" property.

This allows two things:

* Add custom type checking on user defined types:

template RTInfo (T)
{
     static if (T.stringof == "Foo")
         static assert(false, "A type named 'Foo' is not allowed");
     enum RTInfo = null;
}

* Add custom type info for a given type which is accessible at runtime:

template RTInfo (T)
{
     enum RTInfo = T.stringof;
}

The original reason why this was added to the language was to extract 
type info necessary to build a precise garbage collector.

The problem with this is to use this feature you need to modify object.d 
in druntime. The first pull request tries to come up with a solution 
that doesn't require modifying druntime.

The second pull request implemented the same thing as RTInfo but for 
modules instead of types. So the compiler would automatically 
instantiate this template with for each module it encounters during 
compilation.

The main reason for implementing RTInfo for modules is to implement 
reflection. With the template instantiated for each module you could 
scan the module for class and methods and build up reflection data 
completely transparently without the user needing to register any types 
or similar.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list