# Updating a static array, in a nested function without making a temporary array?

**URL:** <https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980>\
**Category:** General Usage\
**Created:** [September 20, 2019, 4:36am UTC](https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980 "2019-09-20T04:36:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hieuphung](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@Hieuphung](https://discourse.julialang.org/u/Hieuphung)\
**Post date:** [September 20, 2019, 4:36am UTC](https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980/1 "2019-09-20T04:36:23Z")

</div>

have been banging my head against a wall trying to use static arrays in julia.

They are fast but updating them is a pain. This is no surprise, they are meant to be immutable!

But it is continuously recommended to me that I use static arrays even though I have to update them. In my case, the static arrays are small, just length 3, and i have a vector of them, but I only update 1 length three SVector at a time.  
thanks

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 20, 2019, 4:40am UTC](https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980/2 "2019-09-20T04:40:11Z")

</div>

some example please?..

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [September 20, 2019, 4:43am UTC](https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980/3 "2019-09-20T04:43:54Z")

</div>

Nowadays you can often get away with converting to `MVector`, modifying the `MVector`, and then converting back to `SVector`, without incurring additional costs (like dynamic allocation). For example,

```julia
using StaticArrays
using BenchmarkTools

function f(x)
    y = MVector(x)
    y[3] = 1
    SVector(y)
end

```

```julia
julia> @btime f(x) setup = (x = rand(SVector{3}))
  1.651 ns (0 allocations: 0 bytes)
3-element SArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 0.9714613510548404
 0.33570294516112464
 1.0

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 20, 2019, 6:43am UTC](https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980/4 "2019-09-20T06:43:44Z")

</div>

[Setfield.jl](https://github.com/jw3126/Setfield.jl) might help

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 20, 2019, 6:58am UTC](https://discourse.julialang.org/t/updating-a-static-array-in-a-nested-function-without-making-a-temporary-array/28980/5 "2019-09-20T06:58:40Z")

</div>

Have you tried `setindex` (no `!`)?

```julia
julia> v = @SVector rand(3)
3-element SArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 0.12239428476400516
 0.32772745126065295
 0.43563988269786025

julia> v_ = setindex(v, 1.5, 2)
3-element SArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 0.12239428476400516
 1.5                
 0.43563988269786025

```
