Look how many of the other bug reports contain the phrase "does not check for" or refer to specific primitive types.
Look how many of the other bug reports contain the phrase "does not check for" or refer to specific primitive types.
``` julia> function f(A::Array) println(A[1:length(A)]) end f (generic function with 1 method)
julia> f([1,2,3,4]) [1, 2, 3, 4]
julia> f(OffsetArray(1:10, -1)) ERROR: MethodError: no method matching f(::OffsetVector{Int64, UnitRange{Int64}}) ```
You could prevent this problem using Julia's type system. The `AbstractArray` might have been too broad. Based on the chronology of the code that might not have been apparent. See other threads for details.
Another way would be to treat `firstindex` as a trait and dispatch on that. ``` julia> f(A::AbstractArray) = f(A, Val(firstindex(A))) f (generic function with 1 method)
julia> f(A::AbstractArray, firstindex::Val{1}) = println(A[1:length(A)]) f (generic function with 2 methods)
julia> f(A::AbstractArray, firstindex::Val{T}) where T = error("Indexing for array does not start at 1") f (generic function with 3 methods)
julia> f(A::AbstractArray, firstindex::Val{0}) = println("So you like 0-based indexing?") f (generic function with 4 methods)
julia> f([1,2,3,4]) [1, 2, 3, 4]
julia> using OffsetArrays
julia> f(OffsetArray(1:10, 1)) ERROR: Indexing for array does not start at 1 Stacktrace: [1] error(s::String) @ Base .\error.jl:33 [2] f(A::OffsetVector{Int64, UnitRange{Int64}}, #unused#::Val{2}) @ Main .\REPL[5]:1 [3] f(A::OffsetVector{Int64, UnitRange{Int64}}) @ Main .\REPL[3]:1 [4] top-level scope @ REPL[9]:1
julia> f(OffsetArray(1:10, -1)) So you like 0-based indexing? ```
But you will still want to calculate indices at runtime, and then out-of-bounds errors will have to be caught at runtime anyway.