# Can/could type inference & specialisation barriers be created at regions other than function calls?

**URL:** <https://discourse.julialang.org/t/can-could-type-inference-specialisation-barriers-be-created-at-regions-other-than-function-calls/59046>\
**Category:** Internals & Design\
**Created:** [April 11, 2021, 6:42pm UTC](https://discourse.julialang.org/t/can-could-type-inference-specialisation-barriers-be-created-at-regions-other-than-function-calls/59046 "2021-04-11T18:42:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xyz](https://avatars.discourse-cdn.com/v4/letter/x/b5a626/32.png) [@xyz](https://discourse.julialang.org/u/xyz)\
**Post date:** [April 11, 2021, 6:42pm UTC](https://discourse.julialang.org/t/can-could-type-inference-specialisation-barriers-be-created-at-regions-other-than-function-calls/59046/1 "2021-04-11T18:42:46Z")

</div>

Type specialisation happens at function calls, which is why the common function-barrier pattern arises.

Speculating a bit: what would be the issue with having some construct that enforces a type-specialisation for a block within the middle of a function? I.e. some statement that, when hit, would pause execution of the function, inspect the types of all variables used in the rest of the function, then specialise the rest of the function on those types.

Perhaps this kind of thing could be achieved quite easily with some macro that generates a closure? Not sure if a closure would have some overhead though - keen to hear from someone who knows this stuff well!

Thanks

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 11, 2021, 8:31pm UTC](https://discourse.julialang.org/t/can-could-type-inference-specialisation-barriers-be-created-at-regions-other-than-function-calls/59046/2 "2021-04-11T20:31:25Z")

</div>

One fairly easy thing would be a macro which splits a function definition into two. In something like:

```julia
@bar function f(x::Int, n::Int)
    y = ntuple(_->x, n)
    @bar
    z = zeros(y...)
    z .+ n
end

```

the first `@bar` can see what variables the second half of the function uses, and expand to

```julia
function f(x::Int, n::Int)
    y = ntuple(_->x, n)
    _f(y, n)
end
function _f(y, n)
    z = zeros(y...)
    z .+ n
end

```

Maybe someone has already written this? I do not think it would have overhead.

---

<div class="post-metadata">

**Author:** ![xyz](https://avatars.discourse-cdn.com/v4/letter/x/b5a626/32.png) [@xyz](https://discourse.julialang.org/u/xyz)\
**Post date:** [April 12, 2021, 8:53pm UTC](https://discourse.julialang.org/t/can-could-type-inference-specialisation-barriers-be-created-at-regions-other-than-function-calls/59046/3 "2021-04-12T20:53:01Z")

</div>

That’s a nice idea. I agree - this does feel like something someone would have tried.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 12, 2021, 9:19pm UTC](https://discourse.julialang.org/t/can-could-type-inference-specialisation-barriers-be-created-at-regions-other-than-function-calls/59046/4 "2021-04-12T21:19:38Z")

</div>

Here’s a simple solution using a macro and do-block syntax to automatically construct a closure which acts as a function barrier:

```julia
macro barrier(f, args...)
    f.args[1].args = [args...]
    esc(:($f($(args...))))
end

function f(x::Int, n::Int)
    y = ntuple(_->x, n)
    @barrier(y) do
        z = zeros(y...)
        z .+ n
    end
end

```
