Po C.
Po C.

Reputation: 688

Type alias for Array with specified dimension

I want to have a type alias, say, MyArrayType{N}, so that Array{T,N} <: MyArrayType{N} for any T. For example,

julia> Array{Float64, 1} <: MyArrayType{1}
true
julia> Array{Float64, 2} <: MyArrayType{1}
false
julia> Array{Float64, 2} <: MyArrayType{2}
true
julia> Array{Integer, 1} <: MyArrayType{1}
true
julia> Array{Any, 1} <: MyArrayType{1}
true
julia> Array{Any, 8964} <: MyArrayType{8964}
true
julia> Array{Any, 2047} <: MyArrayType{8964}
false

I know that there is already a type alias Array{T} which can do Array{T,N} <: Array{T} for any N. Is there already a type alias which can do what I want for the MyArrayType above?

Upvotes: 2

Views: 193

Answers (1)

mbauman
mbauman

Reputation: 31342

As you note, you can always omit trailing type parameters as a short-hand for not restricting them. So by creating a typealias that swaps the order of the parameters, you can omit the array's element type:

julia> typealias MyArrayType{N,T} Array{T,N}
Array{T,N}

julia> Array{Float64, 1} <: MyArrayType{1}
true

julia> Array{Any, 8964} <: MyArrayType{8964}
true

julia> Array{Float64, 2} === MyArrayType{2, Float64}
true

Upvotes: 4

Related Questions