# Setindex for CartesianIndex

**URL:** <https://discourse.julialang.org/t/setindex-for-cartesianindex/36424>\
**Category:** General Usage\
**Tags:** question, first-steps, performance\
**Created:** [March 24, 2020, 8:27am UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424 "2020-03-24T08:27:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)\
**Post date:** [March 24, 2020, 8:27am UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424/1 "2020-03-24T08:27:57Z")

</div>

The question is related to the general topic of mutating immutables.  
StaticArrays comes with a helpful convenience function `setindex`, which allows modifying an array element at a given position and returning a new StaticArray, as far as I understand, presumably this is achieved **without** an additional memory allocation step.  
I wonder if there something equivalent for CartesianIndex? It is a fairly common task (at least within my realm) to try and access nearest-neighbor indices.  
I ended up writing the following function, which returns a CartesianIndex associated with a nearest-neighbor index, using Base.Cartesian macro.

```julia
@generated function hop(index::CartesianIndex{NDIMS},dir::Int64,lr::Int64) where {NDIMS}
    quote
       @ncall $NDIMS CartesianIndex d_t->
       begin
           if d_t==dir
             if (lr==1)
               index[d_t]+1
             else
               index[d_t]-1
             end
           else
             index[d_t]
           end
       end
    end
end

```

My question is whether there is a simpler “Julian” (whatever that means 🙂 ) approach, possibly in the spirit of StaticArray’s `setindex` in the context of CartesianIndex?

Snir

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 24, 2020, 10:27pm UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424/2 "2020-03-24T22:27:31Z")

</div>

Perhaps we should shorten this by defining `setindex` on `CartesianIndex`, but for now

```julia
julia> I = CartesianIndex(1, 2, 3, 4, 5)
CartesianIndex(1, 2, 3, 4, 5)

julia> I = CartesianIndex(Base.setindex(Tuple(I), 50, 3))
CartesianIndex(1, 2, 50, 4, 5)

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 24, 2020, 10:33pm UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424/3 "2020-03-24T22:33:38Z")

</div>

If you want a nice syntax, you can use [https://github.com/jw3126/Setfield.jl](https://github.com/jw3126/Setfield.jl)

```julia
julia> i = CartesianIndex(1, 2, 3)
CartesianIndex(1, 2, 3)

julia> j = @set i.I[2] = 200
CartesianIndex(1, 200, 3)

```

(If `Base.setindex` were implemented it’d be `@set i[2] = 200`)

---

<div class="post-metadata">

**Author:** ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)\
**Post date:** [March 25, 2020, 5:44am UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424/4 "2020-03-25T05:44:23Z")

</div>

Adding this to base would be super nice.

---

<div class="post-metadata">

**Author:** ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)\
**Post date:** [March 25, 2020, 5:45am UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424/5 "2020-03-25T05:45:07Z")

</div>

Thanks! Indeed a nicer syntax.

---

<div class="post-metadata">

**Author:** ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)\
**Post date:** [March 25, 2020, 8:38am UTC](https://discourse.julialang.org/t/setindex-for-cartesianindex/36424/6 "2020-03-25T08:38:50Z")

</div>

Just opened a feature request (naturally low priority but could be nice in the future)

[https://github.com/JuliaLang/julia/issues/35255#issue-587530816](https://github.com/JuliaLang/julia/issues/35255#issue-587530816)
