object.factory with template classes for serializing subclasses automatically
Chris Cain
clcain at uncg.edu
Mon Sep 10 21:24:35 PDT 2012
On Tuesday, 11 September 2012 at 03:18:40 UTC, timotheecour wrote:
> So the question is: is that technically impossible or not to
> enhance Object.factory in such ways?
Unless someone else wants to correct me, I'm going to say
technically impossible. Object.factory constructs a class at
runtime given a string of the name of the class. Templates,
however, are created at compile-time only. Basically, the
compiler creates a new type every time it sees a template being
used with new template arguments.
So, as you can imagine, there doesn't exist any object for
Object.factory to find at run time.
Technically, code like this MIGHT be possible:
---
import std.stdio;
class MyB(T) {
this() {
writeln("hello MyB!(" ~ T.stringof ~")");
}
}
void main() {
// create the actual type at compile-time
Object b = Object.factory(MyB!int.classinfo.name);
writeln("b = ", b);
}
---
In this case, the type (I'd think) is actually created at
compile-time, so it actually exists so that Object.factory can
get it. However, the code you showed in your previous post (just
purely existing in the string) could never work.
More information about the Digitalmars-d-learn
mailing list