Type of composition of functions is not predictable

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

function η_const(r, η_)

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

    elseif r<=0.8

        return convert( typeof(r), 1.0)

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


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

@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
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

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.

1 Like