# Extend a varargs function for mixed types

**URL:** https://discourse.julialang.org/t/extend-a-varargs-function-for-mixed-types/38233
**Category:** General Usage
**Created:** [April 26, 2020, 11:53am UTC](https://discourse.julialang.org/t/extend-a-varargs-function-for-mixed-types/38233 "2020-04-26T11:53:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [April 26, 2020, 11:53am UTC](https://discourse.julialang.org/t/extend-a-varargs-function-for-mixed-types/38233/1 "2020-04-26T11:53:24Z")

</div>

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:

```julia

# 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.

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [April 26, 2020, 2:52pm UTC](https://discourse.julialang.org/t/extend-a-varargs-function-for-mixed-types/38233/2 "2020-04-26T14:52:53Z")

</div>

A possible solution:

```julia
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)

```

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [April 26, 2020, 2:57pm UTC](https://discourse.julialang.org/t/extend-a-varargs-function-for-mixed-types/38233/3 "2020-04-26T14:57:51Z")

</div>

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.
