There is no conversion from Pair to Tuple. The construction:

(a, b, c) = d

works for any iterable collection d. The same holds for the for loop 
construction.

There used to be a type inference issue, but I fixed it in 
https://github.com/JuliaLang/julia/pull/12493. The output of code_warntype 
is a little wonky but you can see it knows the types of k and v here:

      s1 = (Base.box)(Base.Int,(Base.add_int)(s1::Int64,k::Int64)::Any)::
Int64 # none, line 7:
      s2 = (Base.box)(Base.Float64,(Base.add_float)(s2::Float64,v::Float64
)::Any)::Float64

The LLVM IR also indicates it's being optimized correctly.

Simon

On Tuesday, September 8, 2015 at 10:10:21 AM UTC-4, vav...@uwaterloo.ca 
wrote:
>
> 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