Transform `Tuple{Union, NonUnion, Union, ...}` to `Union{Tuple{NonUnion, NonUnion, NonUnion, ...}, ...}`

This is from the I-really-hope-someone-improves-on-this school of problem solving, but

function solutionfunction(T::Type{<:Tuple})
    combinations = collect(Iterators.product(map(Base.uniontypes, fieldtypes(T))...))
    Union{map(c -> Tuple{c...}, combinations)...}
end

is a start:

julia> solutionfunction(Tuple{Union{Int, String}, Bool})
Union{Tuple{Int64,Bool}, Tuple{String,Bool}}

I am not sure if I am supposed to use Base.uniontypes or not.

1 Like