Is there a simple way to construct Union{A,B,C}
from Union{A,B}
and Union{B,C}
? My initial guess was typejoin
, but:
AB = Union{Val{:A}, Val{:B}}
BC = Union{Val{:B}, Val{:C}}
typejoin(AB,BC) == Val
Which I guess is correct, i.e. it is an upper bound. I want the supremum though in this case.
Motivating example: I want to, at compile time, generate the most efficient code to get all the requested work done. Assume there are two jobs: one with job requirements Union{A,B}
and one with job requirements Union{B,C}
. Assume I have a function dojob
with three methods:
dojob(::Type{Union{A,B}}) = (doa(); dob())
dojob(::Type{Union{B,C}}) = (dob(); doc())
dojob(::Type{Union{A,B,C}}) = (doa(); dob(); doc())
In those situations where I need computations A,B, and C done it is clearly more efficient to call the third method than to call the first two in succession (there is some overlap). In order to make sure that this third method is selected at compile time I need to be able to compute the union of the Union types.
For now I have implemented something resembling this functionality using types like Val{:ab}
, Val{:bc}
, … and piggybacking on the promotion infrastructure:
Base.promote_rule(::Type{Val{:ab}}, ::Type{Val{:abc}}) = Val{:abc}
This is unfortunately not scalable and I suspect the Julia type system and in particular the Union
type can give me this for free…