Re: deserialization: creating a class instance without calling constructor

2015-05-25 Thread timotheecour via Digitalmars-d-learn

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!


deserialization: creating a class instance without calling constructor

2015-05-21 Thread Timothee Cour via Digitalmars-d-learn
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.


Re: deserialization: creating a class instance without calling constructor

2015-05-21 Thread John Colvin via Digitalmars-d-learn

On Thursday, 21 May 2015 at 09:06:59 UTC, Timothee Cour 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.


For a start I'm pretty sure you want to be calling 
core.memory.GC.malloc not core.stdc.stdlib.malloc, otherwise you 
leak the memory.


Re: deserialization: creating a class instance without calling constructor

2015-05-21 Thread Baz via Digitalmars-d-learn

On Thursday, 21 May 2015 at 09:06:59 UTC, Timothee Cour 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.


based on this:

https://github.com/D-Programming-Language/druntime/blob/6698ee21d4eb00ec2e8c621993359d235618df75/src/rt/lifetime.d#L71

you can create an instance without calling the constructor like 
this:


---
CT construct(CT, A...)(A a)
if (is(CT == class))
{
auto memory = malloc(typeid(CT).init.length);
memory[0 .. typeid(CT).init.length] = typeid(CT).init[];
return cast(CT) memory;
}
---

actually it only copies the fields with their initial values.




Re: deserialization: creating a class instance without calling constructor

2015-05-21 Thread Jacob Carlborg via Digitalmars-d-learn

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


--
/Jacob Carlborg