Types, strange behavior

Julia prefers to give a specific type to the outer container: vector is an Array{Array{Any,1},1} === Vector{Vector{Any}} rather than Vector{Any}. So the type of each element is Vector{Any}. And a Vector{Int64} is not a subtype of Vectory{Any} so it cannot store [1,2] as Vector{Int64}.

If you prevent Julia from using a specific type for the outer container, it works as you want:

julia> vector = [[1,2], [3,[4,5]], 1]
3-element Array{Any,1}:
  [1, 2]
  Any[3, [4, 5]]
 1

julia> typeof(vector[1])
Array{Int64,1}

or

julia> vector = Any[[1,2], [3,[4,5]]];

julia> typeof(vector[1])
Array{Int64,1}

I think this is the expected behavior. The manual says:

Arrays can also be directly constructed with square braces; the syntax [A, B, C, ...] creates a one dimensional array (i.e., a vector) containing the comma-separated arguments as its elements. The element type ( eltype ) of the resulting array is automatically determined by the types of the arguments inside the braces. If all the arguments are the same type, then that is its eltype . If they all have a common promotion type then they get converted to that type using convert and that type is the array’s eltype . Otherwise, a heterogeneous array that can hold anything — a Vector{Any} — is constructed; this includes the literal [] where no arguments are given.

4 Likes