# Add fill method accepting constructor function

**URL:** <https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424>\
**Category:** General Usage\
**Created:** [January 15, 2020, 11:22pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424 "2020-01-15T23:22:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [January 15, 2020, 11:22pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/1 "2020-01-15T23:22:02Z")

</div>

Initialising array of arrays `fill([], 2)` would end up in an error, as all arrays will be actually the same array.

I suggest provide additional method for fill function, accepting constructor function `fill(() -> [], 2)`.

It’s possible to use for loop comprehension. But adding additional method to `fill` would make `fill` complement.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 15, 2020, 11:57pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/2 "2020-01-15T23:57:06Z")

</div>

So how would you go about filling an array with a function then? You can do `map(i -> [], 1:2)`, however. Or the kinda cryptic `0 .|> fill(i -> [], 2)`.

---

<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:** [January 16, 2020, 9:08am UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/3 "2020-01-16T09:08:36Z")

</div>

I imagine that a function specifically for this, eg named `fillwith` or similar, would be a useful addition to `Base`. I thought I saw some discussion about this an issue, but I cannot find it.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [January 16, 2020, 9:10am UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/4 "2020-01-16T09:10:35Z")

</div>

> [@Tamas\_Papp](#):
>
> I thought I saw some discussion about this an issue, but I cannot find it.

Maybe [WIP/RFC: allow resize! to initialize new elements by rfourquet · Pull Request #16769 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/16769) ?

---

<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:** [January 16, 2020, 9:15am UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/5 "2020-01-16T09:15:36Z")

</div>

Yes, it is related somewhat, but it is my impression that the focus of that is on modfying existing arrays.

I am not sure if adding this to `Base` is worth it though, the general case is

```julia
f.(CartesianIndices((1:3, 1:4)))

```

which already compiles to very neat code when `f` is `_ -> stuff`.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [January 16, 2020, 9:22am UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/6 "2020-01-16T09:22:25Z")

</div>

> [@Tamas\_Papp](#):
>
> f.(CartesianIndices((1:3, 1:4)))

Nice trick!

> [@Tamas\_Papp](#):
>
> but it is my impression that the focus of that is on modfying existing arrays.

Ah you are right.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 16, 2020, 9:24am UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/7 "2020-01-16T09:24:32Z")

</div>

I have never used the `resize!` function directly but the idea of a `copy` keyword would make a lot of sense for `fill`. I.e. `fill([], 2, copy=true)` could make an array with distinct empty arrays.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [January 16, 2020, 12:05pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/8 "2020-01-16T12:05:28Z")

</div>

> [@Alex1](#):
>
> I suggest provide additional method for fill function, accepting constructor function `fill(() -> [], 2)` .

Beside already mentioned problem with defining an array of functions (which is somewhat contrived, as functions defined via `function` or `->` have singleton types), there is a worse one: what if someone makes a callable struct subtyping `Function`? E.g.:

```julia
struct Id{T} <: Function
    value::T
end

(x::Id)() = x.value

```

Why should `fill(Id(0), k)` return an array of zeros and not an array of `Id`s?

---

<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:** [January 16, 2020, 12:09pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/9 "2020-01-16T12:09:21Z")

</div>

> [@Vasily\_Pisarev](#):
>
> problem with defining an array of functions (which is somewhat contrived, as functions defined via `function` or `->` have singleton types)

Not necessarily. Try

```julia
many_functions(n) = [x -> x + i for i in 1:n]
v = many_functions(10) # has homogeneous element types

```

This is essentially equivalent to making a `struct` with `i`, and defining it to be callable.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [January 16, 2020, 2:08pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/10 "2020-01-16T14:08:35Z")

</div>

Ah, my bad, only looked at functions with no captured variables.

Then, I’d better say “an idea of array of functions may seem contrived, but in Julia it’s not uncommon to create entities that may be viewed as both function and data objects”.

---

<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:** [January 16, 2020, 5:05pm UTC](https://discourse.julialang.org/t/add-fill-method-accepting-constructor-function/33424/11 "2020-01-16T17:05:06Z")

</div>

Yep. And of course this power can be abused in creative ways:

```julia
julia> (i::Int)(x) = i + x

julia> (2).([3,5])
2-element Array{Int64,1}:
 5
 7

```
