# Turning off type inference?

**URL:** <https://discourse.julialang.org/t/turning-off-type-inference/28434>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 5, 2019, 3:37pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434 "2019-09-05T15:37:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 5, 2019, 3:37pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/1 "2019-09-05T15:37:05Z")

</div>

Is there a way to turn off type inference? I have a function that’s too keen on making union types that aren’t actually helpful, which causes compilation to take forever (in some cases it seems like I mean literally forever, as I end up ^Cing).

I had been under the wrong impression that adding `::Any` inside a function body or return argument would cause the compiler to lose the type information, but turns out it just checks the type while “remembering” the actual type:

```julia
julia> f(x)::Any = x
f (generic function with 1 method)

julia> @code_warntype(f(1))
Variables
  #self#::Core.Compiler.Const(f, false)
  x::Int64

Body::Int64
1 ─ %1 = Main.Any::Core.Compiler.Const(Any, false)
│ %2 = Base.convert(%1, x)::Int64
│ %3 = Core.typeassert(%2, %1)::Int64
└── return %3

julia> f(x) = x::Any
f (generic function with 1 method)

julia> @code_warntype(f(1))
Variables
  #self#::Core.Compiler.Const(f, false)
  x::Int64

Body::Int64
1 ─ %1 = Core.typeassert(x, Main.Any)::Int64
└── return %1

```

---

<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:** [September 5, 2019, 4:15pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/2 "2019-09-05T16:15:20Z")

</div>

`::ANY` was changed to `@nospecialize`

[https://docs.julialang.org/en/v1/base/base/index.html#Base.@nospecialize](https://docs.julialang.org/en/v1/base/base/index.html#Base.@nospecialize)

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 5, 2019, 9:47pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/3 "2019-09-05T21:47:38Z")

</div>

Hmm, `@nospecialize` seems to be ignored more often than not…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 5, 2019, 9:53pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/4 "2019-09-05T21:53:49Z")

</div>

`@nospecialized` turns off specialization. It does not turn off inference.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 5, 2019, 10:54pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/5 "2019-09-05T22:54:47Z")

</div>

This seems to do what I want, is it a good idea or is there something better?

```julia
struct Uninfer 
    x
end

uninfer(x) = Uninfer(x).x

f(x) = uninfer(x^2)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 5, 2019, 11:01pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/6 "2019-09-05T23:01:17Z")

</div>

It’s a compiler limitations.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 5, 2019, 11:09pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/7 "2019-09-05T23:09:14Z")

</div>

I don’t suppose JuliaInterpreter has a `@interpret` macro…

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 5, 2019, 11:09pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/8 "2019-09-05T23:09:37Z")

</div>

I believe your code there is not always guaranteed to block type inference. Perhaps a better strategy would be to use `Ref{Any}`, as in

```julia
uninfer(x) = Ref{Any}(x)[]

f(x) = uninfer(x)

julia> Core.Compiler.return_type(f, Tuple{Int})
Any

```

This strategy essentially puts your data in a pointer while purposefully telling julia that there could be _anything_ in the pointer and then immediately dereferences the pointer.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 5, 2019, 11:12pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/9 "2019-09-05T23:12:49Z")

</div>

My code seems to work but I like your suggestion better because it doesn’t involve a new type.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 5, 2019, 11:14pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/10 "2019-09-05T23:14:27Z")

</div>

> [@dlfivefifty](#):
>
> I don’t suppose JuliaInterpreter has a `@interpret` macro…

It does indeed: [Home · JuliaInterpreter.jl](https://juliadebug.github.io/JuliaInterpreter.jl/stable/)

Have you tried that?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 5, 2019, 11:16pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/11 "2019-09-05T23:16:53Z")

</div>

It’s exactly the same so it "guarantee"s blocking inference as much as the other one.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 5, 2019, 11:48pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/12 "2019-09-05T23:48:29Z")

</div>

Oh I thought it interprets everything, that is, it doesn’t go back to compile for calls within a block unless they are whitelisted. It also doesn’t mention usage in packages

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 6, 2019, 1:23pm UTC](https://discourse.julialang.org/t/turning-off-type-inference/28434/13 "2019-09-06T13:23:44Z")

</div>

FWIW I tried `@interpret` one case which was narrow enough to only have light computation. It did fix the compile time issue: roughly 0.5s instead of 8s. Unfortunately it eviscerated the run time: 1s instead of 0.0004s. So it’s a no go.
