Return type of function created from eval cannot be inferred?

question

#1

I created two function by eval.

dHx = eval(parse("n->zeros(Float64, n)"))
dHy = eval(parse("n->ones(Float64, n)"))
function dH(n, ind)
    if ind == 1
        return dHx(n)
    else
        return dHy(n)
    end
end

It looks that they should be inferred fine, but

>>>@code_warntype dHx(2)
Variables:
  #self# <optimized out>
  n::Int64

Body:
  begin 
      return $(Expr(:invoke, MethodInstance for fill!(::Array{Float64,1}, ::Float64), :(Base.fill!), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Float64,1}, svec(Any, Int64), Array{Float64,1}, 0, :(n), 0))), :((Base.sitofp)(Float64, 0)::Float64)))
  end::Array{Float64,1}

>>>@code_warntype dH(2, 1)
Variables:
  #self# <optimized out>
  n::Int64
  ind::Int64

Body:
  begin 
      unless (ind::Int64 === 1)::Bool goto 4 # line 3:
      return (Main.dHx)(n::Int64)::Any
      4:  # line 5:
      return (Main.dHy)(n::Int64)::Any
  end::Any

I don’t understand why dH cannot be inferred while dHx and dHy are inferred fine.


#2

This has nothing to do with eval; You are creating anonymous functions so you need to mark them as const dHx = ....