# Avoid re-compiling functions with specialized types

**URL:** https://discourse.julialang.org/t/avoid-re-compiling-functions-with-specialized-types/69057
**Category:** New to Julia
**Created:** [October 1, 2021, 4:45pm UTC](https://discourse.julialang.org/t/avoid-re-compiling-functions-with-specialized-types/69057 "2021-10-01T16:45:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![whatsthecraic](https://avatars.discourse-cdn.com/v4/letter/w/7bcc69/32.png) [@whatsthecraic](https://discourse.julialang.org/u/whatsthecraic)
#### Post date: [October 1, 2021, 4:45pm UTC](https://discourse.julialang.org/t/avoid-re-compiling-functions-with-specialized-types/69057/1 "2021-10-01T16:45:01Z")

</div>

suppose we have the following classes:

```julia
struct A; end
struct B; end
struct C{T}; x; end

function foo(c::C); c.x = 1; end

```

does invoking `foo(C{A}())` and `foo(C{B}())` still cause the compiler to generate two specialized functions, one for A and the other for B ? If so, is there any difference in writing `foo(c::C)` and `foo(c::C{T)) where {T}`, besides the obvious case when T is referred in the body of foo for whatever reason?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 1, 2021, 5:25pm UTC](https://discourse.julialang.org/t/avoid-re-compiling-functions-with-specialized-types/69057/2 "2021-10-01T17:25:37Z")

</div>

> [@whatsthecraic](#):
>
> does invoking `foo(C{A}())` and `foo(C{B}())` still cause the compiler to generate two specialized functions, one for A and the other for B ?

yes. There is a `@nospecialize` macro to avoid specialization, but in general I do not think one wants to avoid that. There should be a very special reason for not wanting that specialization. (see [here](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392))

> [@whatsthecraic](#):
>
> If so, is there any difference in writing `foo(c::C)` and `foo(c::C{T)) where {T}` , besides the obvious case when T is referred in the body of foo for whatever reason?

no
