# How to dispatch on Type Vararg?

**URL:** https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252
**Category:** General Usage
**Tags:** question
**Created:** [November 19, 2019, 6:41am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252 "2019-11-19T06:41:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [November 19, 2019, 6:41am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/1 "2019-11-19T06:41:36Z")

</div>

```julia
julia> Type{Vararg}
ERROR: TypeError: in Type, in parameter, expected Type, got Vararg

```

this seems to be known and explicitly forced [https://github.com/JuliaLang/julia/issues/30995](https://github.com/JuliaLang/julia/issues/30995)

Does someone know an alternative way how I can dispatch on `Vararg`?  
I.e. I have a custom variable `type` which could be assigned wth `Vararg` and know I want to dispatch `f(type::Type{Vararg}) = ...` but this fails with above error.

Any help is highly appreciated

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [November 19, 2019, 6:51am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/2 "2019-11-19T06:51:14Z")

</div>

I just found that `Tuple{Vararg} isa Type{Tuple{Vararg}}`

so using `Tuple{Vararg}` instead of plain `Vararg` works already. EDIT: Does no work, see comment below

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [November 19, 2019, 6:56am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/3 "2019-11-19T06:56:30Z")

</div>

does not differentiates `Vararg` as hoped for, because it turns out `Tuple{Vararg} == Tuple`  
someone any idea?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [November 19, 2019, 10:04am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/4 "2019-11-19T10:04:07Z")

</div>

```julia
The last parameter of a tuple type can be the special type Vararg, which denotes any number of trailing elements

```

Doesn’t this imply that there should be non-Vararg arguments at the beginning?  
So using solely Vararg seems to be not allowed.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [November 19, 2019, 10:14am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/5 "2019-11-19T10:14:57Z")

</div>

See the [interfaces chapter](https://docs.julialang.org/en/v1.2/manual/interfaces/#man-interface-array-1) for examples of methods that use Vararg dispatch.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 19, 2019, 10:31am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/6 "2019-11-19T10:31:38Z")

</div>

You can use something like

```julia
julia> f(::Type{<:Tuple{Vararg}}) = true
f (generic function with 1 method)

julia> f(Tuple{Float64,3}) # example
true

```

for dispatch. But I don’t think you can use `Vararg` in _constructing_ a `Type`. Perhaps consider `NTuple` or something similar.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [November 20, 2019, 6:36am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-type-vararg/31252/7 "2019-11-20T06:36:43Z")

</div>

thank you all for your answers. To clarify: The goal is not to dispatch with Vararg, but ON Vararg. My current workaround is to not dispatch at all, but to use if-else. It seems this is the only way possible right now.

```julia
type = Vararg
function f(type)
  if type === Vararg
    # do something
  else
    # do something else
  end
end
f(type)

```
