Is it possible to specialize on a varargs function if at least one of the arguments is of a certain type?
Maybe best explained with an MWE:
# Assume this is defined in another package which the code below depends on
julia> f(x...) = "generic"
f (generic function with 1 method)
# I would like to do something like this
julia> struct MyType end
julia> f(x::Union{T, Any}...) where T <: MyType = "special"
f (generic function with 1 method)
# Doesn't work (generic method overwritten as seen above)
julia> f(1,2)
"special"
I guess the above doesn’t work because Union{T, Any} === Any
for all T
. I can replace Any
with a set of supported types, but that would only make it so that the specialized version is selected even if MyType is not present.
To add some more context: Here f(x...)
is a quite generic function which may be called by some arbitrary code outside of the control of the module which defines MyType
. The purpose of the module which has MyType
is basically to ‘hook in’ whenever f
is called.