I have an array of functions, and I need to iterate it and pass each element of the array to another function. How can I make it type-stable?
This code illustrates my problem:
function f(g::Function, x::Int64)
return g(x)
end
g1(x) = x^2
g2(x) = (1 - x)^2
garr = [g1, g2]
function test()
for k = 1 : 2
f(garr[k], 20)
end
end
julia> @code_warntype test()
Variables
#self#::Core.Const(test)
@_2::Union{Nothing, Tuple{Int64, Int64}}
k::Int64
Body::Nothing
1 β %1 = (1:2)::Core.Const(1:2)
β (@_2 = Base.iterate(%1))
β %3 = (@_2::Core.Const((1, 1)) === nothing)::Core.Const(false)
β %4 = Base.not_int(%3)::Core.Const(true)
βββ goto #4 if not %4
2 β %6 = @_2::Tuple{Int64, Int64}::Tuple{Int64, Int64}
β (k = Core.getfield(%6, 1))
β %8 = Core.getfield(%6, 2)::Int64
β %9 = Base.getindex(Main.garr, k)::Any
β Main.f(%9, 20)
β (@_2 = Base.iterate(%1, %8))
β %12 = (@_2 === nothing)::Bool
β %13 = Base.not_int(%12)::Bool
βββ goto #4 if not %13
3 β goto #2
4 β return nothing
However, when I run this, this issue doesnβt happen:
function test1()
f(g1, 20)
end
julia> @code_warntype test1()
Variables
#self#::Core.Const(test1)
Body::Int64
1 β %1 = Main.f(Main.g1, 20)::Core.Const(400)
βββ return %1