# Type of composition of functions is not predictable

**URL:** <https://discourse.julialang.org/t/type-of-composition-of-functions-is-not-predictable/81446>\
**Category:** Performance\
**Tags:** question, performance, code\_warntype, type-stability\
**Created:** [May 22, 2022, 4:15am UTC](https://discourse.julialang.org/t/type-of-composition-of-functions-is-not-predictable/81446 "2022-05-22T04:15:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veenty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veenty/32/50940_2.png) [@Veenty](https://discourse.julialang.org/u/Veenty)\
**Post date:** [May 22, 2022, 4:15am UTC](https://discourse.julialang.org/t/type-of-composition-of-functions-is-not-predictable/81446/1 "2022-05-22T04:15:05Z")

</div>

I’m trying to figure out why the type of this function is not predictable

```julia
function η_const(r, η_)

    if r>=1
        return convert( typeof(r), 0.0)

    elseif r<=0.8

        return convert( typeof(r), 1.0)

    else
        return convert( typeof(r), η_((r -0.8)/0.2) )
    end

end

f(r) = quadgk( x-> bump(x, β), -1, (1-2*r^2), rtol=1e-9)[1]

η_ = Fun( x->f(x) , 0..1)

η(r) = η_const(r, η_)

```

If I check

```julia
@code_warntype η(0.5)
MethodInstance for η(::Float64)
  from η(r) in Main at /Users/Veenty/Documents/NYU PhD/Flatiron Institute Research /Spindle_ODE/benchmark_time.jl:50
Arguments
  #self#::Core.Const(η)
  r::Float64
Body::Any
1 ─ %1 = Main.η_const(r, Main.η_)::Any
└── return %1

```

Which does not make sense to me, I would expect for the output to be Float64

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 22, 2022, 6:08am UTC](https://discourse.julialang.org/t/type-of-composition-of-functions-is-not-predictable/81446/3 "2022-05-22T06:08:15Z")

</div>

> [@Veenty](#):
>
> ```julia
> η_ = Fun( x->f(x) , 0..1)
> 
> ```

I’m not sure what is happening on this line, but this does not appear to be valid Julia syntax. It appears that you are attempting to create an anonymous function and assign it to a global variable `η_`. This global variable is referred to in `η(r)`.

For `η(r)` to be type stable, it should not refer to a global variable. The global variable `η_` can change types. Because `η_` change change, it is impossible to infer the type. You should probably make this global a constant.
