object.factory with template classes for serializing subclasses automatically

Chris Cain clcain at uncg.edu
Mon Sep 10 22:57:54 PDT 2012


On Tuesday, 11 September 2012 at 04:47:11 UTC, timotheecour wrote:
>> Also, now that I think about it, why couldn't you do this? 
>> (it's equivalent):
>>
>> auto serialize(T)(T a) {
>>    auto c = cast(SerializerBase) new Serializer!T;
>>    return c.serialize(a);
>> }
>
> that won't work with my example:
> class A{}
> class B:A{int x;}
> A a=new B;
> auto c=serialize(a); => T is A, but we want B so that 
> serialization includes field x.

Ah, I see now.

Well regardless, it couldn't be done so 
conveniently/transparently because Serializable!B wouldn't exist 
in the binary unless it was actually created in code.


More information about the Digitalmars-d-learn mailing list