# Type instability of nested function

**URL:** <https://discourse.julialang.org/t/type-instability-of-nested-function/57007>\
**Category:** New to Julia\
**Created:** [March 12, 2021, 9:12am UTC](https://discourse.julialang.org/t/type-instability-of-nested-function/57007 "2021-03-12T09:12:00Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 12, 2021, 9:17am UTC](https://discourse.julialang.org/t/type-instability-of-nested-function/57007/2 "2021-03-12T09:17:12Z")

</div>

I guess you hit [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)

Current solution is just to put your closure in `let` block

```julia
function test(a)
    b = a
    if a < 0
        b = -a
    end
    let b = b
        f(x) = x + b
        f(10)
    end
end

julia> @code_warntype test(1)
MethodInstance for test(::Int64)
  from test(a) in Main at REPL[3]:1
Arguments
  #self#::Core.Const(test)
  a::Int64
Locals
  b@_3::Int64
  f::var"#f#4"{Int64}
  b@_5::Int64
Body::Int64

```

---

_[View the full topic](https://discourse.julialang.org/t/type-instability-of-nested-function/57007)._
