Hi Nsajko! Thanks for the patient and detailed reply, as always!
I understand that the behavior from my example is fully expected with respect to the current design of the multiple dispatch system. My focus is more on the inconvenience, specifically the code redundancy, caused by such a behavior. If I want to resolve the type ambiguity caused by the first argument, I need to split
foo(::Union{Int, Float64}, ::AbstractArray, ::AbstractArray) = code1
into
foo(::Int, ::AbstractArray, ::AbstractArray) = code1
foo(::Float64, ::AbstractArray, ::AbstractArray) = code1
where the only difference between the two methods is the argument signature. The body of the function (code1
) is repeated twice.
Admittedly, I could write another core function without any constraint on the first argument
foo_core(a1::Any, a2::AbstractArray, a3::AbstractArray) = code1
to be called by those two methods:
foo(a1::Int, a2::AbstractArray, a3::AbstractArray) = foo_core(a1, a2, a3)
foo(a1::Float64, a2::AbstractArray, a3::AbstractArray) = foo_core(a1, a2, a3)
This could be a solution so far.
However, the fact that the user can resolve this type of ambiguity issue by simply doing a manual “union splitting” on the first argument of foo(::Union{Int, Float64}, ::AbstractArray, ::AbstractArray)
made me realize the type ambiguity caused by Union
(of concrete types) is not on the same level of difficulty as many other. Hence, the solution may also not be as drastic as the one @Lilith
proposed.
Specifically, the subtle difference is that, unlike UnionAll
or other abstract types (e.g., Real
), a Union
of concrete types only encloses a finite number of concrete types. In fact, Julia has already implemented union splitting to improve the type stability of function calls. I think a similar strategy can be applied here to eliminate such “type ambiguity.”
Maybe someone from the core development team could enlighten me on the feasibility of realizing union-splitting-based removal of type ambiguity. That would be much appreciated. Nevertheless, I understand that we are not in the 0.X era anymore, so this post is just to fuel my theoretical curiosity about the potential evolution of Julia’s multiple dispatch system. Thanks for reading!