www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Questions about Object and Serialization

reply Ethan Healey <ehealey2007 comcast.net> writes:
Hello all,

I have two questions about features of the D language which are 
currently implemented in the phobos library.

First, why is it that it is just class objects that inherit from the 
Object class? I would like to be able to have an array of Object's like 
in other languages instead of having to use a template class like boxer 
when I have a mixture of base types and class objects.

Second, since the Object class already declares a toString() method for 
all of its subclasses, why not declare serialization methods, or better 
yet, a Serializable or Streamable interface? Also, this should 
definitley all be included in the base types also, as it would be very 
useful to do something like:

int x = 2;
MyObject obj;
Stream s;
x.write(s);
obj.write(s);

I find myself having to write binary data or even normal text data to 
memory buffers a lot, and I think that this would be a good idea. 
Instead of the write() method in Stream being overloaded for each type, 
each class should simply be able to declare its own and also have a 
default write() method that simply writes all of its fields.
Aug 26 2005
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Ethan Healey" <ehealey2007 comcast.net> wrote in message 
news:deohop$2dq1$1 digitaldaemon.com...
 Hello all,

 I have two questions about features of the D language which are currently 
 implemented in the phobos library.

 First, why is it that it is just class objects that inherit from the 
 Object class? I would like to be able to have an array of Object's like in 
 other languages instead of having to use a template class like boxer when 
 I have a mixture of base types and class objects.
D is like C/Java/C++ where primitive types like 'int' and 'double' aren't objects and so they don't share the same syntax. It's a tradeoff between uniformity and performance (and, in D's case, C compatibility). Maybe beefing up boxes is an option. What's wrong with using arrays of boxes to mix ints and objects in the same array? Too verbose?
 Second, since the Object class already declares a toString() method for 
 all of its subclasses, why not declare serialization methods, or better 
 yet, a Serializable or Streamable interface? Also, this should definitley 
 all be included in the base types also, as it would be very useful to do 
 something like:

 int x = 2;
 MyObject obj;
 Stream s;
 x.write(s);
 obj.write(s);

 I find myself having to write binary data or even normal text data to 
 memory buffers a lot, and I think that this would be a good idea. Instead 
 of the write() method in Stream being overloaded for each type, each class 
 should simply be able to declare its own and also have a default write() 
 method that simply writes all of its fields.
Adding int.write(Stream s) would be building the Stream interface into the language which is a very big commitment. It's more flexible architecture to let the stream accept an int. The uniformity of x.write(s) and obj.write(s) would be nice but it isn't worth the downsides IMHO. Your last point about having a default write() method for objects (and possibly structs and checked) so it isn't unreasonable but the downside is that it gets complex when dealing with versioning and marking fields as non-serializable with 'transient'. Some day D might get such a feature, though. I don't know if the RTTI information is rich enough to support deserialization currently.
Aug 27 2005