# Multiple dispatch causes warntype in closure?

**URL:** https://discourse.julialang.org/t/multiple-dispatch-causes-warntype-in-closure/14029
**Category:** Performance
**Created:** [August 25, 2018, 8:04am UTC](https://discourse.julialang.org/t/multiple-dispatch-causes-warntype-in-closure/14029 "2018-08-25T08:04:29Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [August 25, 2018, 8:04am UTC](https://discourse.julialang.org/t/multiple-dispatch-causes-warntype-in-closure/14029/1 "2018-08-25T08:04:29Z")

</div>

Hello, using 0.7.0. I’m trying to do some refactoring by using multiple-dispatch. In the example below, `generator1()` has a “better design” compared to `generator2()`. Unfortunately, `generator1()` causes some **warntype** …

is there any method to avoid the problem? thanks. 😢

```julia
function generator1()::Function

    # multiple-dispatched f() that always return Int64
    function f(x::Vector{Int64})::Int64
        return sum(x)
    end

    function f(x::Matrix{Int64})::Int64
        return prod(x)
    end

    # warntype here ???
    function g(xx)::Int64
        return f(xx) + 1
    end

    return g
end

g1 = generator1()
@code_warntype g1([1, 2])

julia> @code_warntype g1([1, 2])
Body::Int64
14 1 ─ %1 = (Core.getfield)(#self#, :f)::Core.Box │
   │ %2 = (Core.isdefined)(%1, :contents)::Bool │
   └── goto #3 if not %2 │
   2 ─ goto #4 │
   3 ─ $(Expr(:throw_undef_if_not, :f, false)) │
   4 ┄ %6 = (Core.getfield)(%1, :contents)::Any │
   │ %7 = (%6)(xx)::Any │
   │ %8 = (%7 + 1)::Any │
   │ %9 = (Base.convert)(Main.Int64, %8)::Any │
   │ (Core.typeassert)(%9, Main.Int64) │
   │ %11 = π (%9, Int64) │
   └── return %11

##############################
##############################
##############################
function generator2()::Function
    # NOT using multiple dispatch...
    function f_vec(x::Vector{Int64})::Int64
        return sum(x)
    end

    function f_mat(x::Matrix{Int64})::Int64
        return prod(x)
    end

    # need to EXPLICITLY call f_vec() or f_mat()
    function g(xx::Vector{Int64})::Int64
        return f_vec(xx) + 1
    end

    function g(xx::Matrix{Int64})::Int64
        return f_mat(xx) + 1
    end

    return g
end

g2 = generator2()
@code_warntype g2([1, 2]) # FINE

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 26, 2018, 12:31am UTC](https://discourse.julialang.org/t/multiple-dispatch-causes-warntype-in-closure/14029/2 "2018-08-26T00:31:21Z")

</div>

It’s the same issue from this earlier topic you posted: [Strange @code\_warntype - #10 by kristoffer.carlsson](https://discourse.julialang.org/t/strange-code-warntype/14002/10)

You can work around the issue by explicitly capturing `f` in a `let` block:

```julia
julia> function generator1()::Function
           function f(x::Vector{Int64})
               return sum(x)
           end

           function f(x::Matrix{Int64})
               return prod(x)
           end

           g = let f_ = f
             function(xx)
               f_(xx) + 1
             end
           end

           return g
       end

```
