Push! function changes original array

function foo(name, package)
    
    println( package, "#1 foo pack")
    newpackage=copy(package)
    println( newpackage, "#1 foo newpack")

    push!(newpackage[1].second, name)
    println( package, "#2 foo pack")
    println( newpackage, "#2 foo newpack")

    return newpackage
end
julia> foo( "foo", ["A" => ["1"], "B" => ["2"]])
["A" => ["1"], "B" => ["2"]]#1 foo pack
["A" => ["1"], "B" => ["2"]]#1 foo newpack
["A" => ["1", "foo"], "B" => ["2"]]#2 foo pack
["A" => ["1", "foo"], "B" => ["2"]]#2 foo newpack
2-element Array{Pair{String,Array{String,1}},1}:
 "A" => ["1", "foo"]
 "B" => ["2"]

I was sure this function would not touch original input array, but it did.
I really have no idea, I tried collect(), copy() and [:]. What the hell did I miss?

deepcopy instead of copy Will do the job.

3 Likes