Is it possible to iterate over the parameters of a Tuple or Union? Given Tuple{A, B, C, D, E, ...}, is it possible to extract the parameters (A, B, C, D, E, …)?
A partial solution/hack is something like this, for extracting only a single parameter by position:
# Extracts second Tuple parameter in a Tuple type with two elements.
f(::Type{Tuple{S, T}}) where {S <: Any, T <: Any} = T
# Extracts second Tuple parameter.
g(::Type{<:Tuple{<:Any, T}}) where {T <: Any} = T
A related question is: how do I convert an arbitrary tuple type to to a corresponding union, for example Tuple{A, B, C, D} would be converted to Union{A, B, C, D}. Or Perhaps even something like Tuple{A, B, Tuple{C, D}} to the same Union.
I don’t have a real use for this, just being curious.
They are not something users generally need to use. They are used by the compiler because it’s very useful to have a very simple list type that doesn’t have strict types.
reiterating what @Oscar_Smith said …
There really is no good reason for julia developers to ever do anything with svecs other than convert them to a tuple or, if appropriate, a vector. The svec is not a completed data structure – it does what Base requires of it, and does not do whatever else it might. Their use is a fast track to unexpected occurrences.
Another approach to the proposed function:
function eltypesof(x::T) where {T<:Union{Tuple, NamedTuple}}
Tuple( typeof(x).types )
end
function eltypesof(x::Type{T}) where {T<:Union{Tuple, NamedTuple}}
Tuple(x.types)
end
function eltypesof(x::Type{T}) where {T<:NamedTuple}
Tuple(x.types)
end
function eltypesof(x::U) where {U<:Union}
Tuple( Base.uniontypes(x) )
end
eltypesof(::Type{Union{}}) = ()
# or, depending on the intended semantics
# eltypesof(::Type{Union{}}) = Core.TypeOfBottom