On 8/23/2015 8:15 PM, nims wrote:
On Sunday, 23 August 2015 at 03:09:03 UTC, Rikki Cattermole wrote:
Anyway to summise why D doesn't yet have something akin to Java or C#.
Simply put, we generally work with the actual type not an interface.
So libraries like Orange can serialize/deserialize with great
certainty that it got everything.

However if you need any help with making such a library, please let me
know!

Actually I'm coming from C++ but I know a little bit about C# so your
explanation was helpful anyway! Thanks!

As I really need a working serialization library I'll try making one
myself now. However I'm not very experienced with D in general and its
reflection in particular so I could use some help.

I think in order to keep it simple I'll just have the user to write a
function called Serialize() which calls the serializer which then just
writes the values one after the other in a binary file (and, of course,
for arrays and strings the length too). As all classes implement an
interface called Serializable (with the function Serialize()), finding
the right class to serialize won't be too hard (at least I think so).
However then we have to make sure that we deserialize (instantiate) the
right class.

What I was thinking was having a serialize method take an output range which you will just pass in a value.

Does the runtime have a function which give you something like a unique
type id and another one which instantiates the type with the given id?

A hash? Yeah, TypeInfo_Class should. It will take a pointer.
https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L261

Something like:
int id = _magic_runtime_functions.getUniqueTypeId(typeid(T))

This should work

size_t hash = typeid(avalue).getHash(&avalue);

But keep in mind avalue must be an rvalue.

Serializable t = _magic_runtime_functions.createTypeOfId(...)

In order to make this clear I wrote some (untested and unfinished) code
and pushed it into GitHub:
https://github.com/nims1/interface-serialization-d/blob/master/Serializer.d
I know it still has a lot of problems (hopefully, D has a better way of
writing binary files into memory...). It's just a quick draft.

My suggestion would be evaluate the type "down" aka get immutable(char) from immutable(char)[] aka string. So don't try to serialize the string straight. Grab the length put that to the output range, then try to serialize each of the values of the array individually as another function call to itself.

Same sort of deal with other classes, check that they have the interface and if so, call it's serialize method with yourself.

A little confusing I must admit.
Also small tip, std.traits is awesome. Especially isBasicType ;)

Reply via email to