Skip to content

Commit 8fff818

Browse files
mbaumanJeffBezanson
authored andcommitted
Deprecate adding integers to CartesianIndex (#26284)
fixes #26227
1 parent ade40f6 commit 8fff818

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ Library improvements
587587
collection `A`. There are also two other methods with a different API, and
588588
a mutating variant, `replace!` ([#22324]).
589589

590+
* Adding integers to `CartesianIndex` objects is now deprecated. Instead of
591+
`i::Int + x::CartesianIndex`, use `i*one(x) + x` ([#26284]).
592+
590593
* `CartesianRange` changes ([#24715]):
591594
- Inherits from `AbstractArray`, and linear indexing can be used to provide
592595
linear-to-cartesian conversion ([#24715])

base/deprecated.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,12 @@ end
14221422
@deprecate reprmime(mime, x) repr(mime, x)
14231423
@deprecate mimewritable(mime, x) showable(mime, x)
14241424

1425+
# PR #26284
1426+
@deprecate (+)(i::Integer, index::CartesianIndex) (i*one(index) + index)
1427+
@deprecate (+)(index::CartesianIndex, i::Integer) (index + i*one(index))
1428+
@deprecate (-)(i::Integer, index::CartesianIndex) (i*one(index) - index)
1429+
@deprecate (-)(index::CartesianIndex, i::Integer) (index - i*one(index))
1430+
14251431
# PR #23332
14261432
@deprecate ^(x, p::Integer) Base.power_by_squaring(x,p)
14271433

base/multidimensional.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ module IteratorsMD
112112
@inline max(index1::CartesianIndex{N}, index2::CartesianIndex{N}) where {N} =
113113
CartesianIndex{N}(map(max, index1.I, index2.I))
114114

115-
@inline (+)(i::Integer, index::CartesianIndex) = index+i
116-
@inline (+)(index::CartesianIndex{N}, i::Integer) where {N} = CartesianIndex{N}(map(x->x+i, index.I))
117-
@inline (-)(index::CartesianIndex{N}, i::Integer) where {N} = CartesianIndex{N}(map(x->x-i, index.I))
118-
@inline (-)(i::Integer, index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(x->i-x, index.I))
119115
@inline (*)(a::Integer, index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(x->a*x, index.I))
120116
@inline (*)(index::CartesianIndex, a::Integer) = *(a,index)
121117

@@ -279,7 +275,7 @@ module IteratorsMD
279275
@inline function start(iter::CartesianIndices)
280276
iterfirst, iterlast = first(iter), last(iter)
281277
if any(map(>, iterfirst.I, iterlast.I))
282-
return iterlast+1
278+
return iterlast+one(iterlast)
283279
end
284280
iterfirst
285281
end

test/arrayops.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,8 @@ end
16161616
@test I2 + I1 == CartesianIndex((1,8,2))
16171617
@test I1 - I2 == CartesianIndex((3,-2,-2))
16181618
@test I2 - I1 == CartesianIndex((-3,2,2))
1619-
@test I1 + 1 == CartesianIndex((3,4,1))
1620-
@test I1 - 2 == CartesianIndex((0,1,-2))
1619+
@test I1 + 1*one(I1) == CartesianIndex((3,4,1))
1620+
@test I1 - 2*one(I1) == CartesianIndex((0,1,-2))
16211621

16221622
@test zero(CartesianIndex{2}) == CartesianIndex((0,0))
16231623
@test zero(CartesianIndex((2,3))) == CartesianIndex((0,0))

0 commit comments

Comments
 (0)