JuliaArrays / OffsetArrays.jl

Fortran-like arrays with arbitrary, zero or negative starting indices.
Other
195 stars 40 forks source link

`empty(v::OffsetVector)` returns a `Vector` #349

Open matthias314 opened 5 months ago

matthias314 commented 5 months ago

The Julia documentation says that empty(v::AbstractVector) returns "an empty vector similar to v". Currently similar preserves indices for OffsetVector,

julia> v = OffsetVector([1,2], 0:1)
2-element OffsetArray(::Vector{Int64}, 0:1) with eltype Int64 with indices 0:1:
 1
 2

julia> similar(v)
2-element OffsetArray(::Vector{Int64}, 0:1) with eltype Int64 with indices 0:1:
 139997918114896
 139997785055648

but empty does not because it returns a Vector:

julia> empty(v)
Int64[]

Since empty reduces the length of the vector to zero, one can at most preserve either the first or the last index. Assuming that 0-based vectors are the most common use case for OffsetVector, I think it would make sense to preserve the first index in general.

jishnub commented 5 months ago

It is because of this ambiguity that the method isn't defined. Currently,

julia> similar(v, 0)
Int64[]

and empty ends up calling this method.