diff --git a/README.md b/README.md index e7376c776..c95caafad 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `@compat Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()` and `@compat Base.IndexStyle(::Type{<:MyArray}) = IndexCartesian()` to define traits for abstract arrays, replacing the former `Base.linearindexing{T<:MyArray}(::Type{T}) = Base.LinearFast()` and `Base.linearindexing{T<:MyArray}(::Type{T}) = Base.LinearSlow()`, respectively. +* `Compat.collect(A)` returns an Array even on 0.5, no matter what indices the array `A` has. [#21257] + ## Module Aliases * In 0.6, some 0.5 iterator functions have been moved to the `Base.Iterators` @@ -363,3 +365,4 @@ includes this fix. Find the minimum version from there. [#20500]: https://github.com/JuliaLang/julia/issues/20500 [#18629]: https://github.com/JuliaLang/julia/pull/18629 [#21346]: https://github.com/JuliaLang/julia/pull/21346 +[#21257]: https://github.com/JuliaLang/julia/pull/21257 diff --git a/src/Compat.jl b/src/Compat.jl index d672e46fd..3bf039d49 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1471,6 +1471,20 @@ else using Base: StringVector end +# https://github.com/JuliaLang/julia/pull/21257 +if v"0.5.0" <= VERSION < v"0.6.0-pre.beta.28" + collect(A) = collect_indices(indices(A), A) + collect_indices(::Tuple{}, A) = copy!(Array{eltype(A)}(), A) + collect_indices(indsA::Tuple{Vararg{Base.OneTo}}, A) = + copy!(Array{eltype(A)}(map(length, indsA)), A) + function collect_indices(indsA, A) + B = Array{eltype(A)}(map(length, indsA)) + copy!(B, CartesianRange(indices(B)), A, CartesianRange(indsA)) + end +else + const collect = Base.collect +end + include("to-be-deprecated.jl") end # module Compat diff --git a/test/REQUIRE b/test/REQUIRE new file mode 100644 index 000000000..dbe36550b --- /dev/null +++ b/test/REQUIRE @@ -0,0 +1 @@ +OffsetArrays diff --git a/test/runtests.jl b/test/runtests.jl index 8b815e570..11e7a1ba2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1831,6 +1831,15 @@ let x = fill!(StringVector(5), 0x61) @test pointer(x) == pointer(Compat.UTF8String(x)) end +# collect +if VERSION >= v"0.5.0" + using OffsetArrays + a = OffsetArray(1:3, -1:1) + b = Compat.collect(a) + @test indices(b) === (Base.OneTo(3),) + @test b == [1,2,3] +end + include("to-be-deprecated.jl") nothing