The following code loops over a Dict:

function test_paircvt1()
    d = Dict(1=>5.5, 3=>-2.2)
    s1 = 0
    s2 = 0.0
    for (k,v) in d
        s1 += k
        s2 += v
    end
    s1, s2
end
  
Two issues to ask about:

(1) Since eltype(d)==Pair{Int,Float64}, Julia is automatically converting 
Pair{Int,Float64} to Tuple{Int,Float64}.  How is this done?  I looked for 
the appropriate convert() method but couldn't find it. 

(2)  I ran @code_warntype on the above segment and found something 
worrisome: the compiler apparently thinks that both k and v are of type 
Union{Int,Float64}.  Why can't it infer the correct types?  Is there a 
performance loss from the compiler's inability to know the correct types?

The reason I ask is that I have developed SortedDict for the 
DataStructures.jl package, and currently eltype{SortedDict{K,V}) is defined 
to be Tuple{K,V} rather than Pair{K,V} so it is incompatible with Dict. 
 I'm trying to understand the relevant issues in order to fix this.  I am 
running Julia 0.4, 15-day-old master.

Thanks,
Steve Vavasis

Reply via email to