# Force specialization on varargs when the arguments are not all of the same concrete type

**URL:** <https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788>\
**Category:** Performance\
**Created:** [February 18, 2020, 6:18am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788 "2020-02-18T06:18:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [February 18, 2020, 6:18am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788/1 "2020-02-18T06:18:13Z")

</div>

The manual page on Performance Tips ([https://github.com/JuliaLang/julia/blob/master/doc/src/manual/performance-tips.md](https://github.com/JuliaLang/julia/blob/master/doc/src/manual/performance-tips.md)) says:

> This will not specialize:
> 
> ```julia
> f_vararg(x::Int...) = tuple(x...)
> 
> ```
> 
> but this will:
> 
> ```julia
> g_vararg(x::Vararg{Int, N}) where {N} = tuple(x...)
> 
> ```

Unfortunately, this only works if all of the arguments are of the same concrete type.

Is there a way for me to force specialization on varargs when the arguments are not necessarily all of the same concrete type?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 18, 2020, 6:23am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788/2 "2020-02-18T06:23:19Z")

</div>

`h_vararg(x::Vararg{Any, N}) where {N} = tuple(x...)` seems to work

```julia
julia> h_vararg(1,2.0,:three,"four")
(1, 2.0, :three, "four")

julia> (@which h_vararg(1,2.0,:three,"four")).specializations
Core.TypeMapEntry(nothing, Tuple{typeof(h_vararg),Int64,Float64,Symbol,String}, nothing, svec(), 0x0000000000000001, 0xffffffffffffffff, MethodInstance for h_vararg(::Int64, ::Float64, ::Symbol, ::String), true, true, false)

```

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [February 18, 2020, 6:41am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788/3 "2020-02-18T06:41:35Z")

</div>

I notice that both of the following seem to work:

```julia
h_vararg(x::Vararg{Any, N}) where {N} = tuple(x...)

```

```julia
h_vararg(x::Vararg{<:Any, N}) where {N} = tuple(x...)

```

Do you think that there is any difference between the two? Should one be preferred over the other?

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [February 18, 2020, 6:51am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788/4 "2020-02-18T06:51:54Z")

</div>

Also, perhaps this should be added to the documentation?

[https://github.com/JuliaLang/julia/pull/34796](https://github.com/JuliaLang/julia/pull/34796)

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [February 18, 2020, 7:05am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788/5 "2020-02-18T07:05:20Z")

</div>

On a separate note, I have an analogous question about forcing specialization on keyword arguments:

> [@Force specialization on kwargs](https://discourse.julialang.org/t/force-specialization-on-kwargs/34789):
>
> If I have a function like this: f\_vararg(varargs::Int...) = tuple(varargs...) I can force specialization on varargs with either of the following: g\_vararg(varargs::Vararg{Int, N}) where {N} = tuple(varargs...) h\_vararg(varargs::Vararg{\<:Any, N}) where {N} = tuple(varargs...) Now suppose instead that I have a function like this: f\_kwarg(; kwargs...) = (; kwargs...) How can I force specialization on the keyword arguments?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 18, 2020, 7:23am UTC](https://discourse.julialang.org/t/force-specialization-on-varargs-when-the-arguments-are-not-all-of-the-same-concrete-type/34788/6 "2020-02-18T07:23:32Z")

</div>

I don’t know, but my guess is that they are equivalent:

```julia
julia> Tuple{Vararg{<:Any,3}}
Tuple{Any,Any,Any}

julia> Tuple{Vararg{Any,3}}
Tuple{Any,Any,Any}

```
