From 1875947d2bb11a49c63cc67b3f2cc5956488f2d4 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Fri, 7 Dec 2018 01:34:06 -0500 Subject: [PATCH] `get(A::AbstractArray, (), default)` should get `A[]` (#30270) --- NEWS.md | 2 ++ base/abstractarray.jl | 2 +- test/abstractarray.jl | 22 +++++++++++++++++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index c72d0dc3d6586..6c104252a6633 100644 --- a/NEWS.md +++ b/NEWS.md @@ -83,6 +83,8 @@ Standard library changes * New `ncodeunits(c::Char)` method as a fast equivalent to `ncodeunits(string(c))` ([#29153]). * New `sort!(::AbstractArray; dims)` method that can sort the array along the `dims` dimension ([#28902]). * `range` now accept `stop` as a positional argument ([#28708]). + * `get(A::AbstractArray, (), default)` now returns the result of `A[]` if it can instead of always + returning an empty array ([#30270]). * `parse(Bool, str)` is now supported ([#29997]). * `copyto!(::AbstractMatrix, ::UniformScaling)` supports rectangular matrices now ([#28790]). * In `put!(c::Channel{T}, v)`, `v` now gets converted to `T` as `put!` is being called ([#29092]). diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 7fb898a6d215b..6b8f6b92b0bd6 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1166,7 +1166,7 @@ RangeVecIntList{A<:AbstractVector{Int}} = Union{Tuple{Vararg{Union{AbstractRange AbstractVector{UnitRange{Int}}, AbstractVector{AbstractRange{Int}}, AbstractVector{A}} get(A::AbstractArray, i::Integer, default) = checkbounds(Bool, A, i) ? A[i] : default -get(A::AbstractArray, I::Tuple{}, default) = similar(A, typeof(default), 0) +get(A::AbstractArray, I::Tuple{}, default) = checkbounds(Bool, A) ? A[] : default get(A::AbstractArray, I::Dims, default) = checkbounds(Bool, A, I...) ? A[I...] : default function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{AbstractRange,AbstractVector{Int}}, default::T) where T diff --git a/test/abstractarray.jl b/test/abstractarray.jl index ddb1f3fb9d8b2..da449c7c67c76 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -584,11 +584,23 @@ function test_setindex!_internals(::Type{TestAbstractArray}) end function test_get(::Type{TestAbstractArray}) - A = T24Linear([1:24...]) - B = TSlow([1:24...]) - - @test get(A, (), 0) == Int[] - @test get(B, (), 0) == TSlow(Int, 0) + A = T24Linear(reshape([1:24...], 4, 3, 2)) + B = TSlow(reshape([1:24...], 4, 3, 2)) + + @test get(A, (), 0) == 0 + @test get(B, (), 0) == 0 + @test get(A, (1,), 0) == get(A, 1, 0) == A[1] == 1 + @test get(B, (1,), 0) == get(B, 1, 0) == B[1] == 1 + @test get(A, (25,), 0) == get(A, 25, 0) == 0 + @test get(B, (25,), 0) == get(B, 25, 0) == 0 + @test get(A, (1,1,1), 0) == A[1,1,1] == 1 + @test get(B, (1,1,1), 0) == B[1,1,1] == 1 + @test get(A, (1,1,3), 0) == 0 + @test get(B, (1,1,3), 0) == 0 + + @test get(TSlow([]), (), 0) == 0 + @test get(TSlow([1]), (), 0) == 1 + @test get(TSlow(fill(1)), (), 0) == 1 end function test_cat(::Type{TestAbstractArray})