www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reflection: potential possibilities

reply gareis <dhasenan gmail.com> writes:
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.
Jun 07 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iQIVAwUBRmkc27ZlboUnBhRKAQI1Ng/+Ku2fO+KHVYSgkSi445ABIC6/osx0cvCf NzbJmtb37j26MN3jCG9mHCPU8L0C8ru6tSexC2KSWVjeuPWb/OuOm5tabnTF8kT4 MDGPtHWHTJrKyzdPJuhvTq0fTNOhcjfbI4VwnxYsved6sV4Gk++GmaVuhbHJ7IK3 eoqmYBKOAhMmfVXD4CjJLjHUdCSk4A6fMcYcxoiJVXGVVeFKk88Ev2qm5gPhNEWx D+sPvcnvkN3sx7wz1AwGa/J3dLE0ROGih+1HSomsCKYpXS+5Hhfe53te2VIIT3YF whjCww53qjI8HwIsqOYSLW5vI3yXQjlsgv1eKRhY+L8Ts7MDyGvH+mciEkczx1TF uA2Or25pZmQ36Rv+xYTXC5yqSvAms/k2Cb6kgqkMCKkXyP8S7bZBGNmBM1tBLX0z wzsjfal1Wpt6FNBLyNA5Vkd7GpznCsohpieEA/j/XvLSfJqg1Q1T4hndHzwr0LwE NdUILMeEWXXAm6s5LS6NB9tU308lwFnRwalH9taEYD/Rg77ogEeGnXZER+Ek3gpG 9XDoEbtAxXj7/MFLlq5PZPl5fwg44xnifLOIefKbhXw3UboQzI68MIpVm96lQyHl Atgn9yJ+Q0NBcHVdD/SGsylYRT7tHSiGT/Y7YZE4Xm7mDckdoLz4gELFiGAuMkgC rvncawprfXs= =VVM8 -----END PGP SIGNATURE-----
Jun 08 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Jun 08 2007
parent gareis <dhasenan gmail.com> writes:
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.

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.
Jun 08 2007