# Return type of function created from eval cannot be inferred?

**URL:** <https://discourse.julialang.org/t/return-type-of-function-created-from-eval-cannot-be-inferred/12057>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 29, 2018, 8:47am UTC](https://discourse.julialang.org/t/return-type-of-function-created-from-eval-cannot-be-inferred/12057 "2018-06-29T08:47:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [June 29, 2018, 8:47am UTC](https://discourse.julialang.org/t/return-type-of-function-created-from-eval-cannot-be-inferred/12057/1 "2018-06-29T08:47:14Z")

</div>

I created two function by eval.

```julia
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

```julia
>>>@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.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 29, 2018, 9:00am UTC](https://discourse.julialang.org/t/return-type-of-function-created-from-eval-cannot-be-inferred/12057/2 "2018-06-29T09:00:32Z")

</div>

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