I have two custom defined types that I use in my functions, i.e. m::Union{T1,T2}. T1 and T2 have some overlaps with respect to field names and number of fields. I want my functions to deliver a new version of etiher T1 or T2. I don’t want mutating functions here. How to avoid deepcopy() in these cases? If possible, I would avoid multiple dispatch here, i.e. just one function for both T1 and T2.
It’s a bit unclear to me why you’re using deepcopy here – just to get a new instance of T* or because you want to keep some of its fields around? If it’s just about figuring out the type, you can use f(x::T) where T<:Union{T1,T2} = T(...). If the latter, then maybe add a constructor like T1(t::T1; a=nothing, b=nothing) = T1(something(a, t.a), something(b, t.b)) or just use Accessors.jl.
If possible, I would avoid multiple dispatch here, i.e. just one function for both T1 and T2.
Worth noting that dispatch is going to happen regardless, you just save typing a similar function body twice.