Skip to content

Commit 970ce8b

Browse files
authored
require one-based indexing for SizedArray (#845)
Things like iteration currently fail for SizedArrays with offset axes, so I think it's better to error right array. See #844 (comment)
1 parent 0a7f3f7 commit 970ce8b

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/SizedArray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_one_based_indexing(A...) = !Base.has_offset_axes(A...) ||
2+
throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))
13

24
"""
35
SizedArray{Tuple{dims...}}(array)
@@ -15,6 +17,7 @@ struct SizedArray{S<:Tuple,T,N,M,TData<:AbstractArray{T,M}} <: StaticArray{S,T,N
1517
data::TData
1618

1719
function SizedArray{S,T,N,M,TData}(a::TData) where {S,T,N,M,TData<:AbstractArray{T,M}}
20+
require_one_based_indexing(a)
1821
if size(a) != size_to_tuple(S) && size(a) != (tuple_prod(S),)
1922
throw(DimensionMismatch("Dimensions $(size(a)) don't match static size $S"))
2023
end

test/SizedArray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,8 @@
220220
@test x5 == view(Array(x5), :)
221221
end
222222
end
223+
224+
struct OVector <: AbstractVector{Int} end
225+
Base.length(::OVector) = 10
226+
Base.axes(::OVector) = (0:9,)
227+
@test_throws ArgumentError SizedVector{10}(OVector())

0 commit comments

Comments
 (0)