Irregularity in array definition API

I can define an uninitialized array this way:

a = Array{Int32, 2}(undef, 10, 10)

but I can’t define an initialized array the same way:

a = Array{Int32, 2}(42, 10, 10)

I have to use this form instead:

a = fill(Int32(42), 10, 10)

Is there a deeper reason for this? The first form is also used to initialize arrays with ‘nothing’ or ‘missing’, so it overlaps initialized and uninitialized construction category.

What value should the array be initialized with when you do Array{Int32, 2}(42, 10, 10)? Do you want the first value to be used to fill the array? Or should 42 be interpreted as a dimension of the resulting array?

2 Likes

Array{Int,2}(undef, N1, N2) is like Array{Int,2}(undef, (N1,N2)).
Array{Int,2}(42, N1, N2) could be like Array{Int,2}(42, (N1,N2)) which could be as fill(42, (N1,N2)).

1 Like

Array constructors in general are in need of some love.

4 Likes

I see the problem with this option now. As @JeffreySarnoff noted, Array{Int32, 2}(42, (10, 10)) could be the way out.

EDIT

BTW, fill(42, 10, 10) doesn’t make it obvious which arguments are dimensions and which is the value to fill with.

1 Like