# Type inference in closures

**URL:** <https://discourse.julialang.org/t/type-inference-in-closures/12544>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 20, 2018, 8:03pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544 "2018-07-20T20:03:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 20, 2018, 8:03pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/1 "2018-07-20T20:03:04Z")

</div>

Is there any way to make Julia properly infer types in this code:

```julia
function f()
    x = 0
    l = Threads.SpinLock()

    Threads.@threads for i in 1:Threads.nthreads()
        Threads.lock(l)
        x = x + i
        Threads.unlock(l)
    end
    x
end

```

Currently `@code_warntype f()` indicates that `x` is boxed because `@threads` creates a closure. I tried `let` trick, but it does not work in this case.

Does anyone have some advice?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 20, 2018, 8:36pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/2 "2018-07-20T20:36:11Z")

</div>

Using a reference to x allocates (on creating the `Ref`), but it comes up clean in `@code_warntype`:

```julia
f() = f!(Ref(0))
function f!(x::Ref{Int})
    x[] = 0
    l = Threads.SpinLock()
    Threads.@threads for i in 1:Threads.nthreads()
        Threads.lock(l)
        x[] = x[] + i
        Threads.unlock(l)
    end
    x[]
end

```

Seems a little faster.

Maybe there’s a better solution.

Also, the let trick doesn’t really work here, because then the `x` in the closure isn’t updating the `x` outside of it, and I just get `0`.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 20, 2018, 8:43pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/3 "2018-07-20T20:43:11Z")

</div>

Good solution if we know that we are working with `Int`. Thank you.  
However, actually this was a simplified example from a package where the type can be arbitrary.

Then I avoid `Ref` on Julia 0.7 as `Ref(a)` where `a` is an array does not work correctly. Maybe using `Base.RefValue` although it is not exported could be the general approach?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 20, 2018, 8:44pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/4 "2018-07-20T20:44:49Z")

</div>

For more complicated examples, I’d just replace the Ref with a mutable struct that holds everything.  
If something can be entirely arbitrary, I guess make that field parametric?

```julia
mutable struct DefinitelyNotARef{T}
    ref::T
end

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 20, 2018, 8:45pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/5 "2018-07-20T20:45:54Z")

</div>

If the `let` trick is used you put `x` as the last expression in `let` block so it gets returned (but it does not solve the problem here though).

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 20, 2018, 8:46pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/6 "2018-07-20T20:46:42Z")

</div>

Thanks - this is what I ended up doing in the end :).

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 20, 2018, 8:48pm UTC](https://discourse.julialang.org/t/type-inference-in-closures/12544/8 "2018-07-20T20:48:46Z")

</div>

Ah, of course! Should’ve of thought of that.
