josepe | 17 Jul 09:08

[groovy-user] inconsistent behavior with clone() method on a map?


Cloning a map object makes deep copies of some but not other fields

dazed=[buggy:[],ok:0]

//clone and modify copy
confused = dazed.clone()
confused.buggy << "intruder"
confused.ok +=1

//check integrity of original
assert dazed.ok == 0       // ok 
assert dazed.buggy == []  // oops!

--

-- 
View this message in context: http://www.nabble.com/inconsistent-behavior-with-clone%28%29-method-on-a-map--tp18502809p18502809.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 17 Jul 11:41
Gravatar

Re: [groovy-user] inconsistent behavior with clone() method on a map?

josepe schrieb:
> Cloning a map object makes deep copies of some but not other fields
> 
> dazed=[buggy:[],ok:0]
> 
> //clone and modify copy
> confused = dazed.clone()
> confused.buggy << "intruder"
> confused.ok +=1
> 
> //check integrity of original
> assert dazed.ok == 0       // ok 
> assert dazed.buggy == []  // oops!

clone makes usually a shallow copy. There where tons of discussions in 
the Java community about how to solve the problem of deep vs. shallow 
copy In the end there was no real decision as of what to do in general. 
The only thing that they come up with was, that you should have a look 
at the documentation of clone to see what it actually does. Usually it 
will not do a deep clone. If you need a deep cloning, then you have to 
do it by yourself.

side note: this is originally a Java problem, we just haven't found a 
better way for Groovy yet

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
(Continue reading)

josepe | 17 Jul 18:32

Re: [groovy-user] inconsistent behavior with clone() method on a map?


Jochen Theodorou wrote:
> 
> josepe schrieb:
>> Cloning a map object makes deep copies of some but not other fields
>> 
>> dazed=[buggy:[],ok:0]
>> 
>> //clone and modify copy
>> confused = dazed.clone()
>> confused.buggy << "intruder"
>> confused.ok +=1
>> 
>> //check integrity of original
>> assert dazed.ok == 0       // ok 
>> assert dazed.buggy == []  // oops!
> 
> clone makes usually a shallow copy. There where tons of discussions in 
> the Java community about how to solve the problem of deep vs. shallow 
> copy In the end there was no real decision as of what to do in general. 
> The only thing that they come up with was, that you should have a look 
> at the documentation of clone to see what it actually does. Usually it 
> will not do a deep clone. If you need a deep cloning, then you have to 
> do it by yourself.
> 
> side note: this is originally a Java problem, we just haven't found a 
> better way for Groovy yet
> 
> bye blackdrag
> 
(Continue reading)

Jochen Theodorou | 17 Jul 18:48
Gravatar

Re: [groovy-user] inconsistent behavior with clone() method on a map?

josepe schrieb:
[...]
>  I though it was problematic that the copy is not consistently shallow; it
> carries on some of the copied fields but not others. In the example, clone()
> creates a shallow copy of the list field but deep copy of the integer
> object. BTW, the assignment operator "="  does give a shallow copy for both
> fields (both assertions fail). 

I think we had this before... shallow usually means:
(1) if the field is of a primitive type, then the primitive value will 
be copied
(2) if the field object is an object (not primitive), then the reference 
will be copied, but not the object itself, unless the boject is part of 
an internal datastructure that can not be shared betweenmiultiple objects.
(3) if the field object is part of an internal data structure that can 
not be shared with multiple objects, then the object is copied. The idea 
is to replicate the semantic abilities, so there is no need for an exact 
1:1 copy here. An example for this is a table in a HashMap. A clone will 
do referential copies of the values and keys, but it will replicate the 
table, since the table is an internal structure specific to that object.

This is commonly used as shallow copy. In a deep copy the object would 
be copied, but not only the reference.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
(Continue reading)


Gmane