Hi,

When using deepcopy() to copy an immutable type it does not copy everything 
recursively.
There is a discussion here 
<https://groups.google.com/d/msg/julia-users/kug8rdy5JYo/0d9014DugOgJ> in 
which Stefan Karpinski requests a bug be filed resulting in #3301.
But the behavior seems to be the same. So, is deepcopy() supposed to copy 
inner arrays or not?

Here is an example:

First using an non-immutable type
type foo
    myArr::Array{Int64,1}
end

f1 = foo([1,1])
f2 = f1
f3 = deepcopy(f1)
f4 = foo(copy(f1.myArr))


println("f1: $(f1) \nf2: $(f2) \nf3: $(f3) \nf4: $(f4)\n")
# prints...
# f1: foo([1,1]) 
# f2: foo([1,1]) 
# f3: foo([1,1]) 
# f4: foo([1,1])

f2.myArr[1]= 2
f3.myArr[1]= 3
f4.myArr[1]= 4

println("f1: $(f1) \nf2: $(f2) \nf3: $(f3) \nf4: $(f4)\n")
#prints:
# f1: foo([2,1]) 
# f2: foo([2,1]) 
# f3: foo([3,1]) 
# f4: foo([4,1])

The above non-immutable type functions exactly as expected.

Now, the same using immutable types

immutable Immfoo
    myArr::Array{Int64,1}
end

Imm1 = Immfoo([1,1])
Imm2 = Imm1
Imm3 = deepcopy(Imm1)
Imm4 = Immfoo(copy(Imm1.myArr))

println("Imm1: $(Imm1) \nImm2: $(Imm2) \nImm3: $(Imm3) \nImm4: $(Imm4)\n")
# prints...
# Imm1: Immfoo([1,1]) 
# Imm2: Immfoo([1,1]) 
# Imm3: Immfoo([1,1]) 
# Imm4: Immfoo([1,1])

Imm2.myArr[1]= 2
Imm3.myArr[1]= 3
Imm4.myArr[1]= 4


println("Imm1: $(Imm1) \nImm2: $(Imm2) \nImm3: $(Imm3) \nImm4: $(Imm4)\n")
# prints...
# Imm1: Immfoo([3,1]) 
# Imm2: Immfoo([3,1]) 
# Imm3: Immfoo([3,1]) 
# Imm4: Immfoo([4,1])

Here, Imm3 is pointing at the same array as Imm1 and Imm2. 
Is this as intended?

Andre

Reply via email to