Is it possible to define a type that takes a variable number of type parameters

AFAIK there are two types in Julia that can take a variable number of type parameters: Union and Tuple but they both seem to be defined in C. Are there any implemented in Julia or can it not be done?

No, there isn’t currently any way to implement something like this in native Julia, especially since Tuple and Union are also special in many other ways, like now type covariance and the way subtyping works work. If you want to have a custom struct with multiple fields, you can always just use a Tuple as a typed field in your struct and perhaps add a more convenient constructor using splatting.

4 Likes

For a real-life example of a type using a tuple of parameters, as @simeonschaub suggested, you can check out SArray from StaticArrays.jl:

julia> SVector{3, Int}
SArray{Tuple{3},Int64,1,3}

julia> SMatrix{3, 3, Int, 9}
SArray{Tuple{3,3},Int64,2,9}

where the first type parameter of SArray is a tuple of dimensions whose length is equal to the number of dimensions in the array.

5 Likes