If I understand correctly you did something like @code_warntype foo(1.2)
, right? Well, this by itself is not type-unstable. The returned value is always a Vector{Any}
(that is a concrete type), so the “function itself” is type-stable. What is type-unstable is dealing with values obtained from the Vector
:
julia> a = foo(1.2);
julia> @code_warntype a[rand(1:15)]
Variables
#self#::Core.Compiler.Const(getindex, false)
A::Array{Any,1}
i1::Int64
Body::Any
1 ─ %1 = Base.arrayref($(Expr(:boundscheck)), A, i1)::Any
└── return %1
Because then, the type returned is Any
(not Vector{Any}
) and Any
is the root abstract type, so this can be anything.
If you really need a dynamic number of elements of distinct type, there is not much else to do. If every element is a subtype of ABS
then I would suggest changing the function first line to res = Vector{ABS}(undef, 0)
to at least restrict the type a little. Accessing elements will be type unstable anyway (because ABS
is an abstract type) but maybe the compiler can optimize one or other thing.
I suggest you look at this recent answer of mine about how to limit the damage cause by type-instability.