deserialization: creating a class instance without calling constructor

timotheecour via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 25 13:38:39 PDT 2015


On Thursday, 21 May 2015 at 19:06:35 UTC, Jacob Carlborg wrote:
> On 2015-05-21 11:06, Timothee Cour via Digitalmars-d-learn 
> wrote:
>> Can I create an instance of A without calling a constructor? 
>> (see below)
>> Use case: for generic deserialiaztion, when the 
>> deserialization library
>> encounters a class without default constructor for example (it 
>> knows
>> what the fields should be set to, but doesn't know how to 
>> construct the
>> object).
>>
>> class A{
>>   int x=2;
>>   this(int x){
>>     this.x=x;
>>   }
>> }
>>
>>
>> This came up here:
>> https://github.com/msgpack/msgpack-d/issues/54#issuecomment-104136148
>> I provide some hacky solution for that in that thread but I 
>> suspect it's
>> not safe and something is missing.
>
> Here's how I do it in my serialization library Orange [1]
>
> [1] 
> https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L166

Thanks!


More information about the Digitalmars-d-learn mailing list