I see that this is implicit in the discussion of invariant types: Int64 <:
Any but not Dict{Int64, Int64} <: Dict{Any, Any}
So, if you're assigning a value of type Dict{Int64,Int64} to a variable of
type Dict{Any, Any}, you either have to copy it or get a type error.
It's a bit surprising that, if d1 is mentioned for the first time in d1 =
d2 = Dict() in f7 below, the inferred type of d1 is different from the
inferred type of d2, especially since the inferred type is different in d8.
My lesson: don't use cascaded assignments.
On Friday, May 6, 2016 at 11:05:57 AM UTC-7, David Dill wrote:
>
> Oh. I haven't been able to find something in the manual saying that
> collections are copied when variable types don't match.
>
> Here is something closer to what I did, where it's less obvious what's
> going on:
>
> function f7()
> d2::Dict{Int64,Int64}
> d1 = d2 = Dict()
> is(d1, d2)
> end
>
> julia> f7()
> false
>
> I'm used to languages where
> x = y = z()
> is equivalent to
> y = z()
> x = y
>
> But:
> function f8()
> d2::Dict{Int64,Int64}
> d2 = Dict()
> d1 = d2
> is(d1, d2)
> end
>
> julia> f8()
> true
>
> Perhaps the type inference is coming up with a different type for d1 in f8
> vs. f7, so f7 copies but f8 does not.
>
> function f9()
> d1::Dict{Any,Any}
> d2::Dict{Int64,Int64}
> d2 = Dict()
> d1 = d2
> is(d1,d2)
> end
>
> julia> f9()
> false
>
> Are the subtleties of copying conversions and type inference documented
> anywhere?
>
> On Friday, May 6, 2016 at 9:53:22 AM UTC-7, Steven G. Johnson wrote:
>>
>> On Friday, May 6, 2016 at 12:48:20 PM UTC-4, David Dill wrote:
>>>
>>> function f4()
>>> d1::Dict{Any,Any}
>>> d2::Dict{Int64,Int64}
>>> d1 = d2 = Dict()
>>> is(d1, d2)
>>> end
>>>
>>
>> You are forcing a conversion from one type of Dict to another, which
>> makes a copy.
>>
>