I'm very sorry -- I was trying to simplify my example and over-did it, then 
had a typo in my "is".

The behavior is type dependent.  Maybe Julia makes a copy if it infers 
different types?

function f1()
    v = Vector{Dict{Int64, Int64}}(1)
    d1 = v[1] = Dict()
    is(d1, v[1])
end

function f2()
    v = Vector{Dict{Any, Any}}(1)
    d1 = v[1] = Dict()
    is(d1, v[1])
end

julia> f1()
false

julia> f2()
true

On further exploration, the array is not important in the example:

function f3()
    d1 = d2 = Dict()
    is(d1, d2)
end

function f4()
    d1::Dict{Any,Any}
    d2::Dict{Int64,Int64}
    d1 = d2 = Dict()
    is(d1, d2)
end

julia> f3()
true

julia> f4()
false


On Friday, May 6, 2016 at 8:37:37 AM UTC-7, Stefan Karpinski wrote:
>
> u is a dictionary and w is [u], which is an array containing that 
> dictionary:
>
> julia> w = [Dict()]
> 1-element Array{Dict{Any,Any},1}:
>  Dict{Any,Any}()
>
> julia> u = w[1] = Dict()
> Dict{Any,Any} with 0 entries
>
> julia> u
> Dict{Any,Any} with 0 entries
>
> julia> w
> 1-element Array{Dict{Any,Any},1}:
>  Dict{Any,Any}()
>
> julia> u === w[1]
> true
>
> On Fri, May 6, 2016 at 1:36 AM, David Dill <[email protected] 
> <javascript:>> wrote:
>
>> I'm a novice, so forgive me if this is a silly question.  I have made a 
>> reasonable effort to find it in the manual and in google searches.
>>
>> Q: What the heck is going on here, and where are questions like this 
>> answered in the Julia documentation?
>>
>> # One copy of Dict or two?
>> julia> x = y = Dict()
>> Dict{Any,Any} with 0 entries
>>
>> # Exactly what I expect!
>> julia> is(x,y)
>> true
>>
>> # Try assigning to a vector
>> julia> w = [Dict()]
>> 1-element Array{Dict{Any,Any},1}:
>>  Dict{Any,Any}()
>>
>> julia> u = w[1] = Dict()
>> Dict{Any,Any} with 0 entries
>>
>> # WHOA!!  Two copies?
>> julia> is(u, w)
>> false
>>
>>
>

Reply via email to