Cloning in D

Jacob Carlborg doob at me.com
Mon Sep 6 06:36:56 PDT 2010


On 2010-09-06 10:44, Jacob Carlborg wrote:
> On 2010-09-06 05:15, dsimcha wrote:
>> 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?
>
> I can have a look and see what I can do. Could you please report a
> ticket so I don't forget.

I've looked at this problem now but I don't know I can detect the 
aliasing. Suggestions ?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list