# Why should I not "use unnecessary static parameters"

**URL:** <https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859>\
**Category:** General Usage\
**Created:** [August 13, 2020, 7:22am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859 "2020-08-13T07:22:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [August 13, 2020, 7:22am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/1 "2020-08-13T07:22:07Z")

</div>

The style guide says [Don’t use unnecessary static parameters](https://docs.julialang.org/en/latest/manual/style-guide/#Don't-use-unnecessary-static-parameters-1) but does not explain why.  
Is this just a question of style or are there other considerations?

---

<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:** [August 13, 2020, 7:49am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/2 "2020-08-13T07:49:55Z")

</div>

The style guide _does_ explain it:

> especially if `T` is not used in the function body. Even if `T` is used, it can be replaced with [`typeof(x)`](https://docs.julialang.org/en/latest/base/base/#Core.typeof) if convenient. There is no performance difference. Note that this is not a general caution against static parameters, just against uses where they are not needed.

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [August 13, 2020, 7:56am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/3 "2020-08-13T07:56:29Z")

</div>

There is no performance difference, so is this just a caution against typing to much? I did in fact read the style guide 😉 . My question was about an explanation beyond what was in the guide.

---

<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:** [August 13, 2020, 8:00am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/4 "2020-08-13T08:00:04Z")

</div>

> [@HenrikM](#):
>
> so is this just a caution against typing to much?

In a sense, yes. Newcomers to Julia sometimes assume that the redundant `T` in `foo(x::T) where {T}` is needed for performance. Generally it isn’t, but [note the exceptions](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing-1).

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [August 13, 2020, 8:11am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/5 "2020-08-13T08:11:29Z")

</div>

Yes, there is definitely a difference between `foo(x)` and `foo(x::T) where {T}`. But to make it clear, is there some subtle difference between `foo(x::Real)` and `foo(x::T) where {T<:Real}`?

---

<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:** [August 13, 2020, 8:18am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/6 "2020-08-13T08:18:02Z")

</div>

> [@HenrikM](#):
>
> there is definitely a difference between `foo(x)` and `foo(x::T) where {T}`

Not from the perspective of Julia’s compiler (again, given the qualifications above, if you don’t otherwise need `T` etc).

> [@HenrikM](#):
>
> is there some subtle difference between `foo(x::Real)` and `foo(x::T) where {T<:Real}`

No. And `foo(x)` is implicitly `foo(x::Any)`, just replace `Any` with `Real` and it is the same example, just shorter because `Any` is assumed.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [August 13, 2020, 9:59am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/7 "2020-08-13T09:59:19Z")

</div>

There is (or at least used to be) a difference with respect to no-specialize heuristics for higher-order functions: `higher(fun::Function, args...)` gives the compiler license to heuristically decide whether to specialize on `typeof(fun)`. If you instead write `higher(fun::F, args...) where {F<:Function}`, then you force specialization (most of the time the compiler will specialize anyways).

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 13, 2020, 10:12am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/8 "2020-08-13T10:12:03Z")

</div>

Is this the case? The [documentation linked above](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing-1) says that

```julia
g_func(g::Function, num) = ntuple(g, div(num, 2))

```

won’t specialize. Is it a hint rather than a guarantee?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 13, 2020, 10:43am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/9 "2020-08-13T10:43:08Z")

</div>

More of a hint. If you don’t want to specialize, use `@nospecialize` 🙂

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [August 13, 2020, 11:10am UTC](https://discourse.julialang.org/t/why-should-i-not-use-unnecessary-static-parameters/44859/10 "2020-08-13T11:10:41Z")

</div>

Yep, if you don’t want to specialize, use `@nospecialize`, if you absolutely want to specialize, use the `foo(x::T) where T` idiom, and if you trust that the compiler knows best, just write `foo(x)`.
