Skip to content

Commit

Permalink
get(A::AbstractArray, (), default) should get A[] (#30270)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman authored and JeffBezanson committed Dec 7, 2018
1 parent 0ff4253 commit 1875947
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 1875947

Please sign in to comment.