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

Hi all,

does there exist a function which can transform a tuple type of the schema Tuple{Union, NonUnion, Union, ...}, i.e. with Union types on the first level, such that the Union goes out and we have Union{Tuple{NonUnion, NonUnion, NonUnion, ...}, Tuple{NonUnion, NonUnion, NonUnion, ...}, ...}

For example

T = solutionfunction(Tuple{Union{Int, String}, Bool})
T == Union{Tuple{Int, Bool}, Tuple{String, Bool}}

Just wondering, why are you trying to do this?

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)...}

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

I am currently experimenting with type-inference

thanks a lot @Tamas_Papp

I just understood your solution. It is very concise. Thank you very much for your effort!

1 Like