# Help understanding dot assignment to Vector of mutable static arrays

**URL:** <https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108>\
**Category:** General Usage\
**Tags:** staticarrays\
**Created:** [March 28, 2021, 4:06am UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108 "2021-03-28T04:06:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [March 28, 2021, 4:06am UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/1 "2021-03-28T04:06:59Z")

</div>

I am puzzled by the following behavior, which produced a difficult to find bug…

```nohighlight
julia> using StaticArrays

julia> t = @MVector([1.0,2.0])
2-element MVector{2, Float64} with indices SOneTo(2):
 1.0
 2.0

julia> x = zeros(typeof(t),5)
5-element Vector{MVector{2, Float64}}:
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]

julia> x[1] .= t
2-element MVector{2, Float64} with indices SOneTo(2):
 1.0
 2.0

julia> x
5-element Vector{MVector{2, Float64}}:
 [1.0, 2.0]
 [1.0, 2.0]
 [1.0, 2.0]
 [1.0, 2.0]
 [1.0, 2.0]

julia> x[1] .= 0
2-element MVector{2, Float64} with indices SOneTo(2):
 0.0
 0.0

julia> x
5-element Vector{MVector{2, Float64}}:
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]

julia> x[1] = t
2-element MVector{2, Float64} with indices SOneTo(2):
 1.0
 2.0

julia> x
5-element Vector{MVector{2, Float64}}:
 [1.0, 2.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]

```

Could someone please explain why `x[1] .= t` replaces all contents of `x`? I expected it to replace only the contents of `x[1]`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 28, 2021, 5:34am UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/2 "2021-03-28T05:34:08Z")

</div>

You’ve found a bug with `StaticArrays`. Specifically, `x = zeros(typeof(t),5)` produces a `Vector` with `5` of the same object, instead of `5` copies of the `MVector`

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 28, 2021, 5:51am UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/3 "2021-03-28T05:51:47Z")

</div>

`zeros` uses `fill!`, which does not copy arrays. The docstring says “If x is an object reference, all elements will refer to the same object”. All `StaticArrays` does is define `zero(::MVector)`. I’m not sure if this is a bug in `StaticArrays` or a limitation of `zeros` in general for array types.

In this particular example it might be better to use comprehension.

```julia
julia> x = [zero(typeof(t)) for _ in 1:5]
5-element Vector{MVector{2, Float64}}:
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]

julia> x[1] .= t
2-element MVector{2, Float64} with indices SOneTo(2):
 1.0
 2.0

julia> x
5-element Vector{MVector{2, Float64}}:
 [1.0, 2.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 28, 2021, 7:34am UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/4 "2021-03-28T07:34:15Z")

</div>

While others have answered your question (every index of your array is the exact same vector, so mutate one and “all” change because they’re really the same one), a suggestion on your code is that you would likely get better performance using a vector of `SVector`s or [HybridArray](https://github.com/mateuszbaran/HybridArrays.jl)s, as either of these would be one contiguous block of memory, instead of many scattereed pieces.

Somethings would be faster with the vector of (M)vector approach though. Loading/storing a vector would be cheap, so if you do things like swapping them, it may be faster with `MVector`s.  
Of course, setting every element to 0 is fastest in your situation, where multiple indices point to the same one. Or if you do more generally want their contents to be updatable via spooky at-a-distance action like you have here.  
But for most use cases, the vector of svector is or HybridArrays will probably be faster.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 28, 2021, 10:22am UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/5 "2021-03-28T10:22:05Z")

</div>

> [@jishnub](#):
>
> `fill!` , which does not copy arrays.

This is something that causes confusion quite often, and the resulting bugs are hard to track.

Is there any actual known important use of fill that relies on this, or changing this behavior can be considered? (Or simply throwing a method error)

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [March 28, 2021, 5:49pm UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/6 "2021-03-28T17:49:06Z")

</div>

Thanks for all the answers. I was aware of the dangers of `fill` but I did not realize that `zeros` calls `fill`. I do not see this mentioned in the documentation for `zeros`, though there is a “see also” pointing to `fill`. However, I had interpreted that reference to be merely a suggestion for another possible way to create a zero matrix. @elrod: I will replace `MVector` with `SVector` in my code, thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 2, 2021, 12:10pm UTC](https://discourse.julialang.org/t/help-understanding-dot-assignment-to-vector-of-mutable-static-arrays/58108/7 "2021-04-02T12:10:36Z")

</div>

> [@PeterSimon](#):
>
> but I did not realize that `zeros` calls `fill` . I do not see this mentioned in the documentation for `zeros` , though there is a “see also” pointing to `fill` .

It is just an implementation detail, types can implement `zeros` any way they prefer.

FWIW, if `zeros(T, ...)` where `T` is mutable is one of those corner cases where it is hard to decide what to do because both options make sense in some context. It is better to be explicit in code.
