How to understand the keyword `where` in the array type signature

a = Array{Int}[]
push!(a, [1 2; 3 4])
@show typeof(a)
Array{Array{Int64,N} where N,1}

In the above, how should I read where N,1? Especially, what does 1 represent here?

How may I create a variable of type Array{Array{Int64,N} where N,2}?

actually, the 1 goes with the outermost Array, the meaning of this type is:
“Array of 1 dimension, and element of this array is of type Array{Int64, N}”,

the where N means N is not specified, such that it is possible to push vector or even a higher dimensional tensor into a:

julia> push!(a, rand(Bool, 2,2,2))
2-element Vector{Array{Int64, N} where N}:

julia> a[1]
2×2 Matrix{Int64}:

julia> a[2]
2×2×2 Array{Int64, 3}:

Maybe this will also help you understand:

julia> [[1 2;3 4]]
1-element Vector{Matrix{Int64}}:

julia> Matrix{Int64} == Array{Int64,2}

The reason why your original example didn’t return a

Array{Matrix{Int64}, 1}

is because you specifically wanted the flexibility in N:

julia> Array{Int}
Array{Int64, N} where N

Ah! Thanks. Makes sense. Can’t believe I couldn’t catch it :smiley:

1 Like