Reflection: potential possibilities

Daniel Keep daniel.keep.lists at gmail.com
Fri Jun 8 01:41:02 PDT 2007



Thomas Kuehne wrote:
> gareis schrieb am 2007-06-08:
>> Using TypeInfo, ClassInfo, and such, you can gain a fair bit of 
>> knowledge about a class at runtime.
> 
>> You could also modify TypeInfo and ClassInfo. Flectioned does this in 
>> allowing you to redirect functions.
> 
>> You should then be able to create a class at runtime and instantiate it.
> 
>> The problem, of course, is connecting compile-time code to run-time 
>> classes. This gives two options:
>> 1. Use CTFE to create classes based on other classes, or just use mixins 
>> and avoid the creation issue entirely.
>> 2. Use a parent class. Virtual means it just works. (Hopefully.)
> 
>> I haven't tried these, but it sounds like, if this works, and combined 
>> with function arrays, it should be possible to come up with a program 
>> that writes and then runs itself at runtime. (You need function arrays 
>> to support arbitrary numbers of methods per class.)
> 
>> The main issues are:
>>   - Does this work at all? Can I create a ClassInfo / TypeInfo object 
>> and then instantiate from it, or do I always need to go from a 
>> preexisting class? Can I actually modify a ClassInfo object and have the 
>> changes reflected in the program? Flectioned seems to indicate so, since 
>> you can redirect functions.
>>   - If it works, how can I link compile-time code to run-time classes?
> 
>> Any thoughts?
> 
>> Worst case, for my project, I'll have to use CTFE to parse ClassInfo and 
>> then use mixins to create the classes.
> 
> If all your class member functions are functions have the signature
> 	T functionName(TypeInfo[], void*)
> and the dynamic classes use a common base class like:
> 
> # class DynObject : Object{
> # 	T call(char[] name, TypeInfo[], void*);
> # }
> 
> This can be implemented via Flectioned. The problem is how variadic
> functions are implemented in D - especially GDC on 64bit systems.
> 
> Thomas

An alternative might be to use variant arguments; either std.boxer, or
<plug
class="shameless">http://www.prowiki.org/wiki4d/wiki.cgi?DanielKeep/Variant</plug>.

I'd say more, but I'm not entirely sure what you're trying to do...

	-- Daniel


More information about the Digitalmars-d-learn mailing list