Why do views of offsetarrays have one-based indexing if bounds are specified?

The index seems to be one-based or otherwise depending on whether I explicitly specify the lower and upper bounds or leave it implicit. Why is this the case?

julia> a=zeros(-1:1);

julia> b=view(a,-1:1); axes(b)
(Base.OneTo(3),)

julia> b=view(a,:); axes(b)
(Base.IdentityUnitRange(-1:1),)

julia> @which view(a,:)
view(A::AbstractArray, I::Vararg{Any,N}) where N in Base at subarray.jl:153

julia> @which view(a,-1:1)
view(A::AbstractArray, I::Vararg{Any,N}) where N in Base at subarray.jl:153

I would have expected the behavior to be identical. Why is there a difference? Is there a way to retain the index information in the view while specifying the bounds?

Evidently this is how OffsetArrays are sliced, as a similar result is obtained without views.

julia> a=zeros(-1:1)
OffsetArray(::Array{Float64,1}, -1:1) with eltype Float64 with indices -1:1:
 0.0
 0.0
 0.0

julia> axes(a[:])
(Base.IdentityUnitRange(-1:1),)

julia> axes(a[-1:1])
(Base.OneTo(3),)

Indexing in Julia follows† the following rule: if b = a[idx] for some vector index idx, then b[j] = a[idx[j]]. Since -1:1 has axes of Base.OneTo(3), b has the same indexes. This is How It Should Be.

But you can do the following:

julia> using OffsetArrays

julia> a=zeros(-1:1);

julia> b = a[Base.IdentityUnitRange(-1:1)];

julia> axes(b)
(Base.IdentityUnitRange(-1:1),)

julia> axes(a)
(Base.IdentityUnitRange(-1:1),)

†or should follow, any violations are a bug

3 Likes