# Strict typing and restricting types in a method's signature

**URL:** https://discourse.julialang.org/t/strict-typing-and-restricting-types-in-a-methods-signature/645
**Category:** General Usage
**Created:** [November 29, 2016, 3:21pm UTC](https://discourse.julialang.org/t/strict-typing-and-restricting-types-in-a-methods-signature/645 "2016-11-29T15:21:54Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 29, 2016, 3:37pm UTC](https://discourse.julialang.org/t/strict-typing-and-restricting-types-in-a-methods-signature/645/2 "2016-11-29T15:37:59Z")

</div>

> [@Meaning of {T}](https://discourse.julialang.org/t/meaning-of-t/623/7):
>
> The truth is that the compiler will always try to specialize functions, infer types and many other optimizations, but will not always succeed in “reading the mind” of the programmer. The details go beyond current topic and especially current forum section.

This statement is very misleading if not flat out wrong. Type inference problems have nothing to do with function specialization. Function specialization will always occur. Type inference allows the compiler to know in advance which specializations to choose for a function, but even if the concrete types are not known in advance, it will still specialize using the very slow dynamic dispatch. Please try this all out with `@which`, `@code_llvm` and `@code_warntype` to see this in action.

When types are not inferred, there are a bunch of branches with `boxed` variables, and what it’s doing is finding what the type of the variable currently is in order to choose the right dispatch. This is the part that is slow when types are not inferred, and is all “compiled away” when a function is type-stable (you can see it becomes a lot of checks!).

So your logic was in the wrong direction. Julia isn’t magic, and in the end it needs to know the concrete types to call your function with, just like it’s a C-function. But if it cannot “read the mind of the programmer”, i.e. if the program which is making the call is not type-inferrable, then it will type-check until it’s sure of the type, and afterwards dynamically (at run-time) search the methods table for that specialized function and use it. If you are calling lots of small functions (like `+`), this can become a very large cost.

---

_[View the full topic](https://discourse.julialang.org/t/strict-typing-and-restricting-types-in-a-methods-signature/645)._
