# Handy function for making empty arrays

**URL:** <https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241>\
**Category:** Internals & Design\
**Created:** [February 27, 2020, 4:44pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241 "2020-02-27T16:44:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [February 27, 2020, 4:44pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241/1 "2020-02-27T16:44:40Z")

</div>

I am teaching Julia to students with Python background and one thing cough my attention: there are few useful functions for making basic matrices, like `zeros, ones, trues, falses, fill, similar`, but there is no such function which would return empty array (with uninitialized fields). I mean something like, let’s say, `undef(size), undef(Type,size)`. Of course, one may just use `Array{Type}(undef,size)`, but for me it seems to be a strange omission and I wanted to ask about opinions/reasons for this.

Mind you, I am mainly concerned with language accessibility here. Making arrays with ones, zeros, etc. has a nice and easy syntax, but making empty ones (which is common and should be encouraged) requires a command which looks rather strange for an untrained eye. Basically, it is one of the first things you need to teach in Julia, but properly explaining what those curly brackets and undef mean is only possible much later.

---

<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:** [February 27, 2020, 4:59pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241/2 "2020-02-27T16:59:05Z")

</div>

> [@Tbl](#):
>
> one may just use `Array{Type}(undef,size)` , but for me it seems to be a strange omission

There is no omission — in Julia, types are callable and are used to construct things.

Adding another function for the same purpose would be redundant, and inconsistent with the rest of the language.

> [@Tbl](#):
>
> Basically, it is one of the first things you need to teach in Julia

Quite the opposite. `zeros` or `fill` are perfectly fine, and making “uninitialized” arrays is an optimization that arguably does not even belong in a beginner course. The reason for this is that uninitialized arrays can be a source of bugs. `undef` was introduced intentionally to signal this. See

> <https://github.com/JuliaLang/julia/pull/24652>
>
> This pull request replaces the existing primitive \`Array\` constructors with equi…valents requiring \`uninitialized\` as their first argument, e.g. \`Array{T,N}(d::VarArg{Int,N}) -\> Array{T,N}(uninitialized, d::VarArg{Int,N})\`, and reimplements the existing constructors in terms of those new equivalents.
> 
> This pull request is the equivalent of #24400 with \`uninitialized\` in place of \`junk\`, and is the first 1.0-necessary step for #24595.
> 
> Question: \`uninitialized\` is presently a methodless function (as with \`junk\` in #24400), which makes sense only if we plan to simultaneously introduce a convenience constructor \`uninitialized(T, shape...)\`. But if we do not introduce such a convenience constructor, then perhaps \`uninitialized\` should instead be a type. IIRC, whether we should introduce such a convenience constructor hasn't been explicitly decided yet. Thoughts? Thanks and best!

(it was later renamed, [#26316)](https://github.com/JuliaLang/julia/pull/26316))

---

<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:** [February 27, 2020, 5:05pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241/3 "2020-02-27T17:05:40Z")

</div>

If you are interested in the design discussion,

[https://github.com/JuliaLang/julia/issues/24595](https://github.com/JuliaLang/julia/issues/24595)

is a good starting point.

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [February 27, 2020, 5:38pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241/4 "2020-02-27T17:38:29Z")

</div>

I am aware of how constructors work in Julia, by “omission” I meant that we have a set of utility functions performing few similar actions and it may feel like one is missing. I’ve noticed thread #24595 some time ago, I consider this a different topic. But, I’ve missed #24652; one person even had exactly the same idea as I have, thank you.

You are right that `zeros` are fine. Frankly, it is mainly me being frustrated after years of working with Matlab. The fact that that it didn’t even had option to make uninitialized array was driving me nuts.

Anyway, this is a question about rather subtle features. I am fully aware there is no firm right or wrong here.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 29, 2020, 2:45pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241/5 "2020-02-29T14:45:36Z")

</div>

> <https://github.com/JuliaLang/julia/issues/34775>
>
> I find the syntax for creating uninitialized arrays a bit verbose, while there a…re nice and short options for almost all other common cases of creating arrays:
> 
> \`\`\`julia
> \# compare to
> Float64\[1, 2, 3, 4, 5\]
> zeros(Float64, 10)
> ones(Float64, 10)
> fill(1.0, 10)
> \`\`\`
> 
> But for uninitialized arrays you always have to use curly bracket syntax if I'm correct:
> 
> \`\`\`julia
> v = Vector{Float64}(undef, 10)
> \`\`\`
> 
> How about one of these two alternatives, which both seem to be available:
> 
> \`\`\`julia
> v = Float64\[undef, 10\]
> arr = Int32\[undef, 3, 4, 5\]
> 
> \# or
> 
> v = undef(Float64, 10)
> arr = undef(Int32, 3, 4, 5)
> \`\`\`
> 
> The second one is actually easy to get via:
> 
> \`\`\`julia
> (::UndefInitializer)(T::Type, dims::Vararg{Int}) = Array{T}(undef, dims...)
> \`\`\`

I have some sympathy for this position.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 29, 2020, 3:19pm UTC](https://discourse.julialang.org/t/handy-function-for-making-empty-arrays/35241/6 "2020-02-29T15:19:24Z")

</div>

```julia
v = undef(Float64, 10)

```

looks reasonable to me
