Extend a varargs function for mixed types

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.

A possible solution:

struct MyType end
hasmytype() = false
hasmytype(x, rest...) = hasmytype(rest...)
hasmytype(x::MyType, rest...) = true

f(x...) = hasmytype(x...) ? _special_f(x) : _generic_f(x)

Thanks,

This is what I would do if f(x...) was declared in the same package as MyType. In this case f(x...) is in a dependency so I can’t change it.

I have clarified this in the OP.