Thread overview
Reflection: potential possibilities
Jun 08, 2007
gareis
Jun 08, 2007
Thomas Kuehne
Jun 08, 2007
Daniel Keep
Jun 08, 2007
gareis
June 08, 2007
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.
June 08, 2007
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


June 08, 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
June 08, 2007
Daniel Keep wrote:
> 
> 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

Basically a version of this: http://www.ayende.com/projects/rhino-mocks.aspx

It'll be mainly for my own code, which doesn't use varargs, but if I do anything useful with it, I'll release it.

It sounds like it'll take maybe a week to set up the core of it. D is fast :) As well as Thomas Kuehne doing all the work before I got here. Thank you for that.