# Exactly what functions get compiled versions for specialized types?

**URL:** <https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 6, 2017, 1:20pm UTC](https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909 "2017-11-06T13:20:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 6, 2017, 1:20pm UTC](https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909/1 "2017-11-06T13:20:33Z")

</div>

If I define a function like:

```julia
f(x) = x + 1

```

and then call it, `f(1)`, I’ll get a compiled version that is specialized for Ints. Does this apply for all callables?

For example, if I define a closure, `f = (x -> x + a)`, where `a` is a variable defined in the surrounding environment, will `f(1)` get “compiled”? What exactly does it mean here?

Finally, what about callable types? I take this example from the docs,

```julia
julia> struct Polynomial{R}
           coeffs::Vector{R}
       end

julia> function (p::Polynomial)(x)
           v = p.coeffs[end]
           for i = (length(p.coeffs)-1):-1:1
               v = v*x + p.coeffs[i]
           end
           return v
       end

julia> p = Polynomial([1,10,100])
Polynomial{Int64}([1, 10, 100])

julia> p(3)
931

```

Does `p(3)` use a compiled version? What if I create another polynomial, `q = Polynomial([10,14,1])`, and make another call `q(4)`. This triggers a _new_ compiled function? Or does it use the same compiled version created before?

Finally, what about constructors?

As you can see, I am a bit confused about this.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 6, 2017, 1:28pm UTC](https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909/2 "2017-11-06T13:28:58Z")

</div>

> [@e3c6](#):
>
> Or does it use the same compiled version created before?

Since the types match, the same compiled version.

Same applies to constructors. All that matters is the type signature.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 6, 2017, 1:37pm UTC](https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909/3 "2017-11-06T13:37:38Z")

</div>

@Tamas_Papp What about closures? Is there a part of the docs explaining this that I am missing? Thanks!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 6, 2017, 1:42pm UTC](https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909/4 "2017-11-06T13:42:44Z")

</div>

A closure is similar to a call overloaded struct where the closed over variables are the fields.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 6, 2017, 3:48pm UTC](https://discourse.julialang.org/t/exactly-what-functions-get-compiled-versions-for-specialized-types/6909/5 "2017-11-06T15:48:12Z")

</div>

> [@Tamas\_Papp](#):
>
> All that matters is the type signature.

I don’t think that’s true in general. It seems that the compiler [does not specialize over functions](https://github.com/JuliaLang/julia/issues/19137) and over [types](https://github.com/JuliaLang/julia/issues/23471), unless the arguments are typed via parametric `where` syntax. There might be more to it. I [wrote some code](https://github.com/cstjean/TraceCalls.jl/blob/master/src/specializations.jl) to inspect the specializations.

One gotcha is that the compiler will always perform inference using the leaf types, even when it does not generate specialized code for it.
