Hi --
Perhaps I don't know what I am doing, but I have found some deepcopy()
behavior which seems wrong.
Refer to the below code. This is a boiled-down version of behavior I
am seeing in my project code. I create an underlying type, named foo,
containing a vector. Then I wrap it in an upper layer type, named
bar, which is also a vector. (That is, bar is a vector of foos, and
each foo contains a vector of ints.)
Next I create an instance of bar named S. Then I make a deepcopy of S
(called u) and assign things to it. I expect that since I made the
deepcopy, S should not change. However it does. Refer to the session
log which I have also scraped below.
As an aside, when I deepcopy the individual element of foo, then I get
the expected behavior (i.e. S is unchnaged).
Have I misunderstood something? Or is this a bug?
Stuart
----------------------- Code ------------------------------
type foo
a::Int64
b::Vector{Int64}
end
immutable bar
t::Vector{foo}
end
# Constructor
function bar(x::foo...)
t = bar([x...])
end
X = foo(1, [11, 12])
Y = foo(3, [13, 14])
S = bar(X, Y)
S.t[1].b[1]
# u = deepcopy(S.t[1].b[1]) # This works (i.e. S is unchanged)
u = deepcopy(S) # This fails (i.e. S changes)
u.t[1].b[1] = 999
u.t[1].b[1]
S.t[1].b[1]
-------------------- Sesssion log ---------------------
[sdb@localhost examples]$ ../julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation:
http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" to list help topics
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.2.0-1781.r0c597ed5
_/ |\__'_|_|_|\__'_| | Commit 0c597ed56e 2013-06-02 17:05:33
|__/ |
julia> type foo
a::Int64
b::Vector{Int64}
end
julia>
julia> immutable bar
t::Vector{foo}
end
julia>
julia> # Constructor
julia> function bar(x::foo...)
t = bar([x...])
end
bar
julia>
julia> X = foo(1, [11, 12])
foo(1,[11, 12])
julia> Y = foo(3, [13, 14])
foo(3,[13, 14])
julia>
julia> S = bar(X, Y)
bar([foo(1,[11, 12]), foo(3,[13, 14])])
julia>
julia> S.t[1].b[1]
11
julia>
julia> # u = deepcopy(S.t[1].b[1]) # This works (i.e. S is unchanged)
julia> u = deepcopy(S) # This fails (i.e. S changes)
bar([foo(1,[11, 12]), foo(3,[13, 14])])
julia> u.t[1].b[1] = 999
999
julia> u.t[1].b[1]
999
julia>
julia> S.t[1].b[1]
999