# Unable to construct \`Union\` of \`Vararg\`

**URL:** https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995
**Category:** General Usage
**Tags:** type, parametric-types
**Created:** [August 7, 2021, 7:16pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995 "2021-08-07T19:16:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [August 7, 2021, 7:16pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/1 "2021-08-07T19:16:09Z")

</div>

When I tried to construct `Union` of `Vararg` like

```julia
julia> Union{Vararg{Any, 1}, Vararg{Any, 2}}

```

I got

```julia
ERROR: TypeError: in Union, expected Type, got Vararg{Any, 1}

```

but clearly

```julia
julia> Vararg{Any, 1} isa Type
true

julia> [Vararg{Any, 1}, Vararg{Any, 2}] isa Array{<:Type, 1}
true

```

Is this intended behavior and I’m missing something here? Thank you!

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [August 7, 2021, 7:49pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/2 "2021-08-07T19:49:19Z")

</div>

> [@frankwswang](#):
>
> `Vararg{Any, 1} isa Type`

Vararg shouldn’t be a `Type` this is a bug in previous Julia versions and has been fixed in Julia 1.7, See also: [Vararg subtyping is inconsistent between 1.0 and 1.6 · Issue #40405 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/40405#issuecomment-816138691)

* * *

not sure if this can be backported to 1.6 if 1.6 is gonna be LTS tho, @kristoffer.carlsson probably knows

---

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [August 7, 2021, 8:08pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/3 "2021-08-07T20:08:47Z")

</div>

Thank you, Roger! Then if I want to realize a similar goal here, do you know there’s a way to do that? Specifically, I want to control the allowed `N` in Vararg{T, N} as the type of the argument type for a function in an efficient way. For example, only allow the number of arguments to be 1,2,3,4.

I know I can at least do it by:

```julia
foo(a1=nothing, a2=nothing, a3=nothing, a4=nothing) = []

```

Thank you!

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [August 7, 2021, 8:14pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/4 "2021-08-07T20:14:35Z")

</div>

note, `Vararg` means less than `N` arguments, thus it doesn’t make sense to have `Union{Vararg{Any, 1}, Vararg{Any, 2}}` either

```julia
julia> foo(xs::Vararg{Int, 4}) = xs
foo (generic function with 1 method)

julia> foo(1,2,3,4)
(1, 2, 3, 4)

julia> foo(1,2,3,4, 5)
ERROR: MethodError: no method matching foo(::Int64, ::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
  foo(::Int64, ::Int64, ::Int64, ::Int64) at REPL[1]:1
Stacktrace:
 [1] top-level scope
   @ REPL[3]:100: 

```

---

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [August 7, 2021, 8:27pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/5 "2021-08-07T20:27:14Z")

</div>

I think `Vararg{T, 4}` means “exactly `N` type-`T` arguments”?

```julia
julia> foo(xs::Vararg{Int, 4}) = xs
foo (generic function with 1 method)

julia> foo(1,2,3,4)
(1, 2, 3, 4)

julia> foo(1,2,3)
ERROR: MethodError: no method matching foo(::Int64, ::Int64, ::Int64)

```

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [August 7, 2021, 8:30pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/6 "2021-08-07T20:30:19Z")

</div>

oh yeah, you are right, that’s exactly `N` arguments.

then you can do the following

```julia
julia> for N in [1, 4]
           @eval foo(xs::Vararg{Int, $N}) = xs
       end

julia> foo(1)
(1,)

julia> foo(2)
(2,)

julia> foo(2, 3)
ERROR: MethodError: no method matching foo(::Int64, ::Int64)
Closest candidates are:
  foo(::Int64, ::Int64, ::Int64, ::Int64) at REPL[8]:2
  foo(::Int64) at REPL[8]:2
Stacktrace:
 [1] top-level scope
   @ REPL[11]:1

```

---

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [August 7, 2021, 8:34pm UTC](https://discourse.julialang.org/t/unable-to-construct-union-of-vararg/65995/7 "2021-08-07T20:34:52Z")

</div>

This seems like an alternative solution. Thank you!
