Should we make `IndexStyle(::Type{<:AbstractVector})` returns `IndexLinear()` by default?

Currently, we have

eachindex(A::AbstractVector) = axes1(A)

But the default IndexStyle of AbstractVector is IndexCartesian()
The following example looks a little strange to me

julia> a = view([1,2,3],[1,2,3])
3-element view(::Vector{Int64}, [1, 2, 3]) with eltype Int64:
 1
 2
 3
julia> IndexStyle(a)
IndexCartesian()
julia> eachindex(a)
Base.OneTo(3)

Some functions like Base.copyto_unaliased! is dispatched based on IndexStyle. So I think the consistency between IndexStyle and eachindex also helps to pick the correct branch.

1 Like

eachindex Is maybe a bad example here, because it uses linear indexing for any dimension of array.

julia> rand(10, 10) |> eachindex
Base.OneTo(100)

julia> rand(10, 10, 10) |> eachindex
Base.OneTo(1000)

I agree this would be nice, but IIRC last I looked at it (likely four+ years ago) there were ambiguity challenges because it means that every custom array needs to specify both their vector and arbitrary dimension behaviors.

Looks like IndexStyle for Base’s Array types are all defined using Type{}
Maybe we can add IndexStyle(::AbstractVector) = IndexLinear() to partially realize the goal? (IndexStyle(typeof(v))) is rarely used I think)

It would be pretty bad if IndexStyle(a) and IndexStyle(typeof(a)) returned different things. Traits like IndexStyle are meant to be specific to a type; that you can call them also with an instance of that type is just for convenience.

3 Likes