Type instability of array getindex

The following example shows that there is a runtime dispatch on accessing an array. Why is that the case, and can I avoid it?

struct example
    a::Array{Int64}
end

function examplefun(x::example)
    return @inbounds x.a[10]
end
julia> @report_opt examplefun(example([1,2]))
═════ 1 possible error found ═════

│┌ getindex(A::Array{Int64}, i::Int64) @ Base ./essentials.jl:916
││ runtime dispatch detected: Base.throw_boundserror(A::Array{Int64}, %25::Tuple{Int64})
│└────────────────────

array is an abstract type. did you want Vector{Int}?

3 Likes

Thank you! That solves it. I was confused thinking Array{Int} was the concrete type of the corresponding AbstractArray abstract type. I see now, I need to use Vector, Matrix, or specify Array dimensions.

2 Likes