# Zeros, ones, array construction redesign

**URL:** https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149
**Category:** Internals & Design
**Created:** [November 18, 2017, 1:54pm UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149 "2017-11-18T13:54:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 18, 2017, 1:54pm UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149/1 "2017-11-18T13:54:43Z")

</div>

First off, I’d like to say I think that I love the detailed approach that @sacha has taken, laying out different goals, different proposals, and the pros/cons for the different possibilities.

I was surprised that it was decided to keep `ones` and `zeros`, though, just for creating `Float64` arrays though, since it’s only 2 characters difference between `fill(0.,d)` `zeros(d)`, and 3 for `fill(1.,d)` & `ones(d)`.  
When I first came to Julia, I was surprised that the default for `zeros` and `ones` was `Float64` (not coming from any language that had `ones` or `zeros`), and it seems that `fill` is both more consistent and easier to understand for new people coming to Julia, since the type of the elements is explicit (and then one can easily generalize to things like `fill(0,d)` or `fill(d128"1",d)`)  
Another advantage, is that whereas the type is hidden with `ones` and `zeros`, only using `fill` would make it easier for things like the very nice new [ChangePrecision](https://github.com/stevengj/ChangePrecision.jl) package to find and modify those literals (currently, `ones`, `zeros`, and `eye` have to be special cased)

If the reason for keeping those functions is simply for Matlab compatibility, couldn’t those simply be added to a Matlab compatibility package? Julia is so much better than Matlab or R, it would be sad to see any of the warts of those languages carried over for all time into Julia, IMO.

---

<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: [November 18, 2017, 5:02pm UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149/2 "2017-11-18T17:02:18Z")

</div>

> [@ScottPJones](#):
>
> 2 characters difference between `fill(0.,d) zeros(d)`, and 3 for `fill(1.,d)` & `ones(d)`

But this violates the style guidelines, so really, it’s

```julia
zeros(d)
fill(0.0, d)
ones(d)
fill(1.0, d)

```

And more generally,

```julia
zeros(T, d)
fill(zero(T), d)

```

By the way, could you point me to where the discussions on array construction is taking place? I’m a bit curious.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 18, 2017, 5:58pm UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149/3 "2017-11-18T17:58:59Z")

</div>

> [@DNF](#):
>
> By the way, could you point me to where the discussions on array construction is taking place? I’m a bit curious.

[design of array constructors · Issue #24595 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/24595) ([Deprecate `ones`? · Issue #24444 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/24444) and [Functions that return arrays with eltype as input should use container type instead? · Issue #11557 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/11557) are also relevant)

---

<div class="post-metadata">

### Author: ![sacha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sacha/32/2663_2.png) [@sacha](https://discourse.julialang.org/u/sacha)
#### Post date: [November 18, 2017, 8:52pm UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149/4 "2017-11-18T20:52:25Z")

</div>

@ScottPJones, thanks for the kind words!

@DNF, please see [#24595](https://github.com/JuliaLang/julia/issues/24595) for detailed analysis of the associated issues. That analysis includes — as a small point dwarfed by greater considerations — a more thorough analysis of the relative length of `ones`/`zeros` and `fill`. (In short, the length differential goes both ways in practice and is usually marginal in any case.)

Best!

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 20, 2017, 2:06pm UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149/5 "2017-11-20T14:06:22Z")

</div>

Actually, I wasn’t really concerned about whether it was 2, 3, or 4 characters extra in those cases, it seems to me a small price to pay to have the base language have one consistent and generic method, instead of a couple Matlab legacy names (which I don’t want eliminated, just moved, along with some other Matlab functions that don’t have a good Julian name, such as cumsum/cumprod, to a Matlab compatibility package, so the only change for people who want those names would be to add `using Matlab` or some such, to their code).

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 25, 2017, 8:41am UTC](https://discourse.julialang.org/t/zeros-ones-array-construction-redesign/7149/6 "2017-11-25T08:41:41Z")

</div>

I would like to add something (tiny) to the discussion `fill` vs `zeros` if that’s ok.  
It would bother me (slightly) to use `fill(0.0, d)` to initialize an array of zeros as I find the name `fill` suboptimal for that purpose. It describes an _action_ rather than an _object_ and the usecase for `zeros` is initializing an object, namely an array. I can totally see why we have `fill!(A, x)` because it does what it says: it fills an _existing_ array with something. But `A = fill(0.0, d)` seems a bit odd from a users perspective, because it doesn’t really _fill_ anything that has existed before. `zeros` was different in this regard because it does what it says: it creates a _new_ array of zeros for you. In this spirit, maybe it should rather be `A = filled(0.0, d)` or similar.

Also, for sparse matrices we will still have `spzeros` (fill doesn’t make too much sense here I guess) and there would be an inconsistency to be able to do `A = spzeros(d)` but not `A = zeros(d)`.

(Other example: `diagm`. It describes an object, not an action.)

I hope you get my (probably unimportant) point and apologies if this is not the right place to comment on this or if this has already been discussed somewhere else (there are quite a number of threads on this or similar issues).
