Type stability of vector of matrices

See Be aware of when Julia avoids specializing in the manual.

In particular, if you want the code to specialize on a type argument, you often need to be more explicit:

function stabilized(d, ::Type{T}) where T
    return [zeros(T,d,d) for _ = 1:d]
end

This code doesn’t run, because R = Vector{Matrix{T}} assigns R to the type itself, not an instance of the type. I guess you mean R = Vector{Matrix{T}}(undef, d)

(I’m not sure why this and the other example you showed are type stable … I wouldn’t count on this unless you use the ::Type{T} trick. Maybe it depends on whether the function gets inlined, which is decided heuristically by the compiler?)

5 Likes