Parametric struct with key arguments

struct SortedParticleList{N,M,T<:Unsigned}
    storage::SVector{N,T}
end

Here we have a parametric struct with three position arguments. I can have

SortedParticleList{N}

and

SortedParticleList{N, M}

and

SortedParticleList{N,M,T}

but not

SortedParticleList{M}

right? Can we have key arguments for struct definition?

No, but you can create aliases. Some common aliases in use are Vector and Matrix.

const Vector{T} = Array{T,1}
const Matrix{T} = Array{T,2}

I’ve have wanted a N version of this. You could write that as follows.

const NArray{N,T} = Array{T,N}

In use, this would be as follows.

julia> const NArray{N,T} = Array{T,N}
Array{T, N} where {N, T}

julia> NArray{1}
Vector (alias for Array{T, 1} where T)

julia> NArray{2}
Matrix (alias for Array{T, 2} where T)

julia> NArray{3}
Array{T, 3} where T

Another way to write this as follows.

julia> const MArray{M} = Array{<: Any,M}
Array{<:Any, M} where M

julia> MArray{2}{Int}
Matrix{Int64} (alias for Array{Int64, 2})

julia> MArray{2, Float64}
Matrix{Float64} (alias for Array{Float64, 2})

So you could do as follows.

julia> const SortedParticleListM{M, T, N} = SortedParticleList{N, M, T}
SortedParticleListM (alias for SortedParticleList{N, M, T} where {M, T, N})

julia> SortedParticleListM{1}
SortedParticleListM{1} (alias for SortedParticleList{N, 1, T} where {T, N})
2 Likes