# fill(anArray,2) behaviour

**URL:** <https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429>\
**Category:** General Usage\
**Created:** [March 27, 2019, 8:35pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429 "2019-03-27T20:35:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [March 27, 2019, 8:35pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429/1 "2019-03-27T20:35:45Z")

</div>

I have doubts about the usefulness of `fill(anArray,2)`. The use case for the current behaviour seems limited (who needs 2 copies of the same array in an array?) and the potential for being misleading is considerable (falsely assuming that these two arrays can be independently changed).

Still, I admit that there might be reasons of internal logic for this behaviour. If so, I suggest a careful documentation. (Yes…I fell into this trap today. )

Example:

```julia
X = fill(zeros(2),2)
X[1][1] = 99

```

gives

```julia
 X
2-element Array{Array{Float64,1},1}:
 [99.0, 0.0]
 [99.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 27, 2019, 9:17pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429/2 "2019-03-27T21:17:33Z")

</div>

I felt into this trap in the past as well. To get different arrays you can do:

```julia
[zeros(2) for i in 1:2]

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 27, 2019, 9:27pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429/3 "2019-03-27T21:27:53Z")

</div>

The reason is simply that is is precisely the same as the behavior of

```julia
X[1] = y
X[2] = y

```

combined with the fact that julia does not implicitly copy things.

I agree that this can be confusing at first, but, to be fair, this exact issue is already specifically outlined in the documentation of `fill()`, so I’m not sure how much more documentation is really needed.

In a perfect world, I might have chosen a more descriptive name (would “repeat” be better?).

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [March 27, 2019, 9:40pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429/4 "2019-03-27T21:40:07Z")

</div>

> [zeros(2) for i in 1:2]

Thanks. That is indeed the way to do it.

> this exact issue is already specifically outlined in the documentation of `fill()` ,

I believe the documentation says `If x is an object reference, all elements will refer to the same object.` I must admit that this was not entirely easy for me to parse.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 27, 2019, 9:57pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429/5 "2019-03-27T21:57:48Z")

</div>

If someone wants to improve that documentation, it would be helpful to give an example like the one in your original post here.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [March 27, 2019, 10:01pm UTC](https://discourse.julialang.org/t/fill-anarray-2-behaviour/22429/6 "2019-03-27T22:01:20Z")

</div>

Fair enough. I’ll read up on how to do that (no, I don’t know) and then give it a try.
