Skip to content

Commit 9daaed6

Browse files
authored
add CartesianIndex() for Ref getindex and setindex! (#32653)
* add CartesianIndex() for Ref getindex and setindex! * fix typo * add NEWS.md entry * move definitions later in the load sequence
2 parents 6e4acaa + 617bd1d commit 9daaed6

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ New library functions
3232
to search for that character in a string passed as the second argument ([#31664]).
3333
* New `findall(pattern, string)` method where `pattern` is a string or regex ([#31834]).
3434
* `istaskfailed` is now documented and exported, like its siblings `istaskdone` and `istaskstarted` ([#32300]).
35+
* `RefArray` and `RefValue` objects now accept index `CartesianIndex()` in `getindex` and `setindex!` ([#32653])
3536

3637
Standard library changes
3738
------------------------

base/multidimensional.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,3 +1743,6 @@ function _sortslices(A::AbstractArray, d::Val{dims}; kws...) where dims
17431743
B
17441744
end
17451745
end
1746+
1747+
getindex(b::Ref, ::CartesianIndex{0}) = getindex(b)
1748+
setindex!(b::Ref, x, ::CartesianIndex{0}) = setindex!(b, x)

test/abstractarray.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,11 @@ end
987987
@test_throws DimensionMismatch promote_shape(Dims((2, 2)), Dims((2,)))
988988
@test_throws DimensionMismatch promote_shape(Dims((2, 3, 1)), Dims((2,2)))
989989
end
990+
991+
@testset "getindex and setindex! for Ref" begin
992+
for x in [Ref(1), Ref([1,2,3], 1)]
993+
@test getindex(x) == getindex(x, CartesianIndex()) == 1
994+
x[CartesianIndex()] = 10
995+
@test getindex(x) == getindex(x, CartesianIndex()) == 10
996+
end
997+
end

0 commit comments

Comments
 (0)