# Why doesn't multi-dimensional Array have zero-argument constructor?

**URL:** <https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [August 30, 2023, 1:25pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367 "2023-08-30T13:25:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [August 30, 2023, 1:25pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/1 "2023-08-30T13:25:27Z")

</div>

I can create an empty one-dimensional array with a zero-argument constructor:

```julia-repl
julia> Vector{Float64}()
Float64[]

```

But I can’t create an empty _multi-dimensional_ array with a zero-argument constructor:

```julia-repl
julia> Array{Float64,2}()
ERROR: MethodError: no method matching Matrix{Float64}()
...

```

Is there a reason why the zero-argument constructor is not implemented for multi-dimensional arrays? We can certainly create empty multi-dimensional arrays by

```julia-repl
julia> Float64[;;]
0×0 Matrix{Float64}

julia> Float64[;;;]
0×0×0 Array{Float64, 3}

```

so I don’t think there is any fundamental obstacle to implementing it, but I’m not sure.

---

<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:** [August 30, 2023, 1:32pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/2 "2023-08-30T13:32:11Z")

</div>

There is a unique empty vector (with a given element type), but there are many different empty n-dimensional arrays for `n > 1`. E.g.

```julia
julia> fill(0.0, 0, 1, 2)
0×1×2 Array{Float64, 3}

julia> fill(0.0, 2, 1, 0)
2×1×0 Array{Float64, 3}

```

Emptiness is determined by one of the dimensions being zero, but the other dimensions can have any size at all. For `n = 1` this dictates that the one dimension have size zero, but for larger `n` the other dimensions are undetermined.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 30, 2023, 1:48pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/3 "2023-08-30T13:48:21Z")

</div>

You can also `push!` to an empty vector, which makes it much more common to use an empty one.

---

<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:** [August 30, 2023, 1:52pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/4 "2023-08-30T13:52:44Z")

</div>

> [@StefanKarpinski](#):
>
> Emptiness is determined by one of the dimensions being zero, but the other dimensions can have any size at all. For `n = 1` this dictates that the one dimension have size zero, but for larger `n` the other dimensions are undetermined.

But one could say that there is one _canonical_ empty array, where all dimensions are zero, and use that.

---

<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:** [August 30, 2023, 3:27pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/6 "2023-08-30T15:27:37Z")

</div>

> [@DNF](#):
>
> But one could say that there is one _canonical_ empty array, where all dimensions are zero, and use that.

Agreed, but then there’s also the zero-dimensional corner case where there is _no_ empty array.

---

<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:** [August 30, 2023, 4:21pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/7 "2023-08-30T16:21:19Z")

</div>

That would be a different type, though.

---

<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:** [August 30, 2023, 5:25pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/8 "2023-08-30T17:25:27Z")

</div>

Well, yes, `Array{T, N}` for different `T` and `N` are all different types, so I’m not sure I see your point.

---

<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:** [August 30, 2023, 9:32pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/9 "2023-08-30T21:32:32Z")

</div>

I’m not sure I understood yours either to be honest:

> [@GunnarFarneback](#):
>
> Agreed, but then there’s also the zero-dimensional corner case where there is _no_ empty array.

I took this to mean that one possible zero-dimensional array would be the one where there is no value at all, the absence of an object/value, and that you considered that case as an alternative to 0x0 as the canonical case for `Array{Float64, 2}()`. And then I responded to that by saying that would not preserve the type of the (zero-argument) constructor.

But I perhaps misunderstood?

---

<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:** [August 30, 2023, 9:40pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/10 "2023-08-30T21:40:32Z")

</div>

My point was just that if the idea of the zero-argument constructor is to create an empty array, then `Array{T, 0}` would be a corner case since they don’t have any empty instances.

---

<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:** [August 30, 2023, 9:49pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/11 "2023-08-30T21:49:11Z")

</div>

Ah, zero-_dimensional_, not zero-_size_. Yeah, not sure about that one.

---

<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:** [September 5, 2023, 1:05pm UTC](https://discourse.julialang.org/t/why-doesnt-multi-dimensional-array-have-zero-argument-constructor/103367/12 "2023-09-05T13:05:35Z")

</div>

I think the issue this highlights is that if you’re going to have a convention for a specific empty array being “the standard one” then it’s a bit of an odd smell that in one of the cases (zero dimensions) there is no such array. It’s one of those cases of “if you do this it works… oh but not in this one case, then it doesn’t.” If something can’t really be a consistent convention maybe it shouldn’t be a convention in the first place.

Interestingly, for a given value, there is a unique single-element array of every dimensionality containing just that value because there’s only one way for the product of n dimensions to be 1.
