Cloning in D

dsimcha dsimcha at yahoo.com
Sun Sep 5 20:15:58 PDT 2010


I've started playing around with Orange a little to see whether it would meet
D's cloning needs.  IMHO one must-have feature for proper cloning that truly
"just works" is full aliasing preservation.  For example, the following code
modified slightly from the Orange example doesn't work properly:

import orange._; // import the whole library

class A
{
    int[] arr1;
    int[] arr2;

    equals_t opEquals (Object other)
    {
        if (auto a = cast(A) other)
            return a.arr1 == this.arr1 && a.arr2 == this.arr2;

        return false;
    }
}

void main ()
{
    auto a = new A; // create something to serialize
    a.arr1 = [1,2,3,4,5];
    a.arr2 = a.arr1[1..$];

    auto serializer = new Serializer!(XMLArchive!());
    auto data = serializer.serialize(a);

    println(data);

    auto a2 = serializer.deserialize!(A)(data);
    assert(a == a2);

    a2.arr2[0] = 0;
    println(a2.arr1);  // [1,2,3,4,5]
}

Note that Orange gets this right for class references that point to the same
object, but not for arrays that overlap.

A few questions:

1.  Are most serialization libraries in other languages capable of getting
this right?

2.  Do others agree that full aliasing preservation is essential with regard
to array slices?

3.  Jacob, do you think you could fix Orange to support this properly?


More information about the Digitalmars-d mailing list