# Poll: Do you use convenience functions such as \`zeros\` to construct OffsetArrays?

**URL:** <https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377>\
**Category:** General Usage\
**Tags:** arrays, type-piracy, offsetarrays\
**Created:** [July 18, 2022, 5:19am UTC](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377 "2022-07-18T05:19:10Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [July 18, 2022, 5:19am UTC](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377/1 "2022-07-18T05:19:10Z")

</div>

Recently, it [has been pointed out](https://github.com/JuliaArrays/OffsetArrays.jl/issues/306) that the type-piracy committed by `OffsetArrays` can lead to unexpected results. In particular, `OffsetArrays` provides the convenience method `zeros(::Union{Integer, AbstractUnitRange}...)` to construct an offset array of zeros, e.g. as

```julia
julia> zeros(3:4)
2-element OffsetArray(::Vector{Float64}, 3:4) with eltype Float64 with indices 3:4:
 0.0
 0.0

```

Similar methods are added to `ones`, `trues`, `falses` and `fill` to return `OffsetArrays`, so, e.g.,

```julia
julia> fill(4, 5:7)
3-element OffsetArray(::Vector{Int64}, 5:7) with eltype Int64 with indices 5:7:
 4
 4
 4

```

Despite the convenience offered by such methods, they commit type-piracy, and we’re in the middle of deciding whether such methods should be retained in the next breaking release of `OffsetArrays`. I would like to hear from users of `OffsetArrays` if they feel that these should be retained.

Do you use any of the convenience functions `zeros`, `ones`, `trues`, `falses` or `fill` to construct `OffsetArray`s?

_Poll ([view on site](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377/1))_

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 18, 2022, 8:22am UTC](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377/2 "2022-07-18T08:22:48Z")

</div>

My suggestion is that `OffsetArrays` should not extend `Base.zeros`. There could be an `OffsetArrays.zeros` though that is not exported. If someone wants `OffsetArrays.zeros` they should explicitly import it.

However, I really think something like this should be the syntax: `OffsetArray{Int}(zeroinit, 3:4)` where `const zeroinit = Fill(0)`.

---

<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:** [July 18, 2022, 8:49am UTC](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377/3 "2022-07-18T08:49:48Z")

</div>

> [@mkitti](#):
>
> `OffsetArray{Int}(zeroinit, 3:4)` where `const zeroinit = Fill(0)`.

I quite like this syntax, and it is available as well. My one concern is that, in analogy with the `Base` method

```julia
julia> Array{Missing,2}(missing, 2, 2)
2×2 Matrix{Missing}:
 missing missing
 missing missing

```

should `Fill(0)` be the scalar element, or should it be `0`? I suppose a special `FillInitializer` type may declare that the element is the scalar. I wonder why such an initializer isn’t available in `Base`?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 18, 2022, 8:53am UTC](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377/4 "2022-07-18T08:53:33Z")

</div>

> [@jishnub](#):
>
> I wonder why such an initializer isn’t available in `Base`?

Because you run into the same problem as with the existing `fill` (which I’m sure you can find discussion & issues about):

```julia
julia> mutable struct A
           x::Int
       end

julia> arr = fill(A(1), 3)
3-element Vector{A}:
 A(1)
 A(1)
 A(1)

julia> arr[1] === arr[2]
true

```

Should that `FillInitializer` copy or not?

---

<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:** [July 18, 2022, 8:59am UTC](https://discourse.julialang.org/t/poll-do-you-use-convenience-functions-such-as-zeros-to-construct-offsetarrays/84377/5 "2022-07-18T08:59:34Z")

</div>

I’d say that it shouldn’t. A `FillInitializer` should not behave differently from `fill`. It’ll only facilitate dispatch while constructing arrays

Edit: I found [an issue with some discussion](https://github.com/JuliaLang/julia/pull/41562). In any case, `OffsetArrays.jl` probably isn’t the right place to introduce a filling initializer.
