Is it documented anywhere that I can create an array like this?

I was reading about Array initialization, and it talks about using the undef keyword for an uninitialized array. However, if you leave out the undef keyword and pass an iterable, you can create an array, for example:

numbers = Array{Int}([1,2,3,4,5])
5-element Array{Int32,1}:
 1
 2
 3
 4
 5

This is a conversion. Such as from a float array:

julia> numbers = Array{Int}(Float64[1,2,3,4,5])                                                                                                                                           
5-element Array{Int64,1}:                                                                                                                                                                 
 1                                                                                                                                                                                        
 2                                                                                                                                                                                        
 3                                                                                                                                                                                        
 4                                                                                                                                                                                        
 5     
2 Likes

Yea this is actually just a conversion. You are constructing the array with [1,2,3,4,5]

Is it documented anywhere that it’s a conversion? Your code obviously demonstrates it, but it seems I stumbled on conversion and thought it was initialization.

Essentially it is a special case of getting a T from a T(x) method, so

https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Conversion

1 Like