Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matthias314 opened this issue Mar 31, 2024 · 1 comment
Open

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

matthias314 opened this issue Mar 31, 2024 · 1 comment

Comments

@matthias314
Copy link
Contributor

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
Copy link
Member

jishnub commented Apr 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants