Constrain type to variable sized union

I want to constrain a type parameter to be a variable sized union whose elements are concrete types of another parametric type. For example:

struct Foo{T}

struct Bar{T <: Union{Vararg{Foo}}}

This doesn’t work because Vararg isn’t a type. Also the reason I don’t just use a::Foo without a type parameter is because I want T to be a small Union of concrete types itself. This is so that the compiler can use union splitting to optimize rather than have a field with an arbitrary abstract type.

Unions don’t have a size per se (not one dispatchable in your code anyway, without manually distinguishing cases), so the safest way to do this is to explicitly state the types you want to Union in the Union.

Subsets of that Union should still be subtypes of the larger one regardless, though I have not checked explicitly.

Do you have a specific application of this in mind?

It looks like

struct Bar{T <: Foo}

should do what you want, based on the behavior of

julia> Union{Complex{Int64},Complex{Float64}} <: Complex