Handy function for making empty arrays

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.

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.

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

(it was later renamed, #26316))

1 Like

If you are interested in the design discussion,

https://github.com/JuliaLang/julia/issues/24595

is a good starting point.

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.

I have some sympathy for this position.

2 Likes
v = undef(Float64, 10)

looks reasonable to me

8 Likes