Skip to content

Commit dfe0cc0

Browse files
committed
Merge pull request #13811 from JuliaLang/teh/more_linearslow
Fix `first` for LinearSlow arrays
2 parents 41c7b64 + bb9c833 commit dfe0cc0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

base/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ndims{T,n}(::Type{AbstractArray{T,n}}) = n
6060
ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T))
6161
length(t::AbstractArray) = prod(size(t))::Int
6262
endof(a::AbstractArray) = length(a)
63-
first(a::AbstractArray) = a[1]
63+
first(a::AbstractArray) = a[first(eachindex(a))]
6464

6565
function first(itr)
6666
state = start(itr)

test/abstractarray.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,17 @@ function test_ind2sub(::Type{TestAbstractArray})
370370
end
371371
end
372372

373+
# A custom linear slow array that insists upon Cartesian indexing
374+
type TSlowNIndexes{T,N} <: AbstractArray{T,N}
375+
data::Array{T,N}
376+
end
377+
Base.linearindexing{A<:TSlowNIndexes}(::Type{A}) = Base.LinearSlow()
378+
Base.size(A::TSlowNIndexes) = size(A.data)
379+
Base.getindex(A::TSlowNIndexes, index::Int...) = error("Must use $(ndims(A)) indexes")
380+
Base.getindex{T}(A::TSlowNIndexes{T,2}, i::Int, j::Int) = A.data[i,j]
381+
382+
383+
373384
type GenericIterator{N} end
374385
Base.start{N}(::GenericIterator{N}) = 1
375386
Base.next{N}(::GenericIterator{N}, i) = (i, i + 1)
@@ -478,3 +489,8 @@ test_map_promote(TestAbstractArray)
478489
test_UInt_indexing(TestAbstractArray)
479490
test_vcat_depwarn(TestAbstractArray)
480491
test_13315(TestAbstractArray)
492+
493+
A = TSlowNIndexes(rand(2,2))
494+
@test_throws ErrorException A[1]
495+
@test A[1,1] == A.data[1]
496+
@test first(A) == A.data[1]

0 commit comments

Comments
 (0)