diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 690cc80629273..fdfaa1d9500ff 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -60,7 +60,7 @@ ndims{T,n}(::Type{AbstractArray{T,n}}) = n ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T)) length(t::AbstractArray) = prod(size(t))::Int endof(a::AbstractArray) = length(a) -first(a::AbstractArray) = a[1] +first(a::AbstractArray) = a[first(eachindex(a))] function first(itr) state = start(itr) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index bd152586b9abd..0a8c332324043 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -370,6 +370,17 @@ function test_ind2sub(::Type{TestAbstractArray}) end end +# A custom linear slow array that insists upon Cartesian indexing +type TSlowNIndexes{T,N} <: AbstractArray{T,N} + data::Array{T,N} +end +Base.linearindexing{A<:TSlowNIndexes}(::Type{A}) = Base.LinearSlow() +Base.size(A::TSlowNIndexes) = size(A.data) +Base.getindex(A::TSlowNIndexes, index::Int...) = error("Must use $(ndims(A)) indexes") +Base.getindex{T}(A::TSlowNIndexes{T,2}, i::Int, j::Int) = A.data[i,j] + + + type GenericIterator{N} end Base.start{N}(::GenericIterator{N}) = 1 Base.next{N}(::GenericIterator{N}, i) = (i, i + 1) @@ -478,3 +489,8 @@ test_map_promote(TestAbstractArray) test_UInt_indexing(TestAbstractArray) test_vcat_depwarn(TestAbstractArray) test_13315(TestAbstractArray) + +A = TSlowNIndexes(rand(2,2)) +@test_throws ErrorException A[1] +@test A[1,1] == A.data[1] +@test first(A) == A.data[1]