User-defined Variadic Parametric Types

In Base, Tuple can have variadic (0 or more) type arguments.

Is there a way to achieve this for a user defined type?

I want to represent something like a strongly typed reference to a single database row. Where the struct itself holds only a reference to the database and a row number, and the type parameters are for dynamic dispatch and to ensure only available data can be accessed.

(In C++ I would probably use a variadic template for this)

1 Like

You can parameterize a type by a tuple type.

2 Likes

Could you please give an example? Are you suggesting something like this?:

struct RowReference{T} where T <: Tuple 
   vals::T
end

This doesn’t solve the problem since I don’t want to store instances on the struct itself. I could have a field which is a Tuple{Vararg{DataType}} but I want to use this to resolve some stuff at compile time, so that adds unwanted overhead.

What I want is something more like this (not actual Julia syntax):

struct RowReference{Ts...}
   db::DB
   rowID::Int 
end

function getindex(r::RowReference{Ts...}, col::U) where U in Ts
    # Look up col number in DB index, grab the element from the DB
end 

StaticArrays use a tuple to give one parameter per axis.

1 Like