On Tuesday, 8 September 2015 at 19:30:16 UTC, chris stevens wrote:
On Sunday, 6 September 2015 at 14:45:45 UTC, BBasile wrote:
You have Object.factory for this. You can also use a custom
factory based on string comparison. (with some: static
if(condition) return new This; else static if(otherCondition)
return new That; etc).
I just had a look at Object.factory and this isn't actually
what I wanted. I was looking for something that would allow me
to create new (previously undefined) classes in D at runtime
that I could then use with Object.factory to create instances
of.
I think I can do this with Variants and dynamic, is this
possible? Or is there another way?
No, it's not possible with variants. What you want to do is
actually complex and won't be solved here.
To create a new class instance, the runtime needs the TypeInfo
class for the class type. Why ? because for example if in a class
declaration you declare an int initially equal to 1, the TypeInfo
class provide the memory layout that matches to this initital
value.
For example this is how class instances are creates in D:
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L71
---
extern (C) Object _d_newclass(const ClassInfo ci)
---
without the 'ClassInfo' parameter, you can't get the initial
state of a class.
This is what the runtime needs to create a class instance.