# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.88878703,"math_prob":0.884116,"size":1004,"snap":"2022-05-2022-21","text_gpt3_token_len":251,"char_repetition_ratio":0.12,"word_repetition_ratio":0.033898305,"special_character_ratio":0.24701196,"punctuation_ratio":0.11165048,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97659874,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-18T10:23:11Z\",\"WARC-Record-ID\":\"<urn:uuid:d08970c7-1e5e-47b4-9aba-726fbcbce54e>\",\"Content-Length\":\"23641\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0936d22b-59e9-44d5-8eb6-b2114822cb34>\",\"WARC-Concurrent-To\":\"<urn:uuid:3d1ae86d-7569-4eb2-8eb1-96d44491e624>\",\"WARC-IP-Address\":\"64.71.144.205\",\"WARC-Target-URI\":\"https://discourse.julialang.org/t/extend-a-varargs-function-for-mixed-types/38233\",\"WARC-Payload-Digest\":\"sha1:DWCPKK7M7LTTEE57XZRHSN2N37WU74Q6\",\"WARC-Block-Digest\":\"sha1:AQFFIGYIBCYY25AICBN4YP2YBSZGG2YH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662521883.7_warc_CC-MAIN-20220518083841-20220518113841-00402.warc.gz\"}"}