Consider the following two functions:

function f1(t, a)
    [t(x) for x in a]
end

function f2(t, a)
    Dict((x, t(x)) for x in a)
end

When I run @code_warntype on them I get:

julia> @code_warntype f1(sin, [1.0, 2.0])
Variables:
  #self#::#f1
  t::Base.#sin
  a::Array{Float64,1}

Body:
  begin
      return $(Expr(:invoke, LambdaInfo for 
collect(::Base.Generator{Array{Float64,1},Base.#sin}), :(Base.collect), 
:($(Expr(:new, Base.Generator{Array{Float64,1},Base.#sin}, :(t), :(a))))))
  end::Array{Float64,1}

julia> @code_warntype f2(sin, [1.0, 2.0])
Variables:
  #self#::#f2
  t::Base.#sin
  a::Array{Float64,1}
  #1::##1#2{Base.#sin}

Body:
  begin
      #1::##1#2{Base.#sin} = $(Expr(:new, ##1#2{Base.#sin}, :(t)))
      SSAValue(0) = #1::##1#2{Base.#sin}
      SSAValue(1) = $(Expr(:new, 
Base.Generator{Array{Float64,1},##1#2{Base.#sin}}, SSAValue(0), :(a)))
      return $(Expr(:invoke, LambdaInfo for 
Dict{K,V}(::Base.Generator{Array{Float64,1},##1#2{Base.#sin}}), 
:(Main.Dict), SSAValue(1)))
  end::Union{Dict{Any,Any},Dict{Float64,Float64},Dict{Union{},Union{}}}

I have the folowing questions:

   1. Why when f2 is used as written above the return type is not 
   Dict{Float64,Float64} (like in the case of f1 where it is 
   Array{Float64,1})?
   2. How to fix f2 so that Julia can infer the return type?
   3. Is there a way to change f2 so that first empty Dict{<element type of 
   a>,<element type of t(a)>}() variable is created, where <element type of 
   a> and <element type of t(a)> are determined based on the passed 
   arguments, and only next this dictionary is populated with data?

With kind regards,
Bogumil Kaminski

Reply via email to