# Add varargs... to generic function

**URL:** <https://discourse.julialang.org/t/add-varargs-to-generic-function/84046>\
**Category:** General Usage\
**Tags:** functions\
**Created:** [July 11, 2022, 10:42am UTC](https://discourse.julialang.org/t/add-varargs-to-generic-function/84046 "2022-07-11T10:42:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [July 11, 2022, 10:42am UTC](https://discourse.julialang.org/t/add-varargs-to-generic-function/84046/1 "2022-07-11T10:42:20Z")

</div>

Is there a function that can take any kwargs function and return the same function with a varargs… parameter? That is

convert  
`f(; a ) = 1`  
to  
`f(; a, X... ) = 1`

and  
`f(; a,b,c ) = a+b+c`  
to  
`f(; a,b,c, X... ) = a+b+c`

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 11, 2022, 11:37am UTC](https://discourse.julialang.org/t/add-varargs-to-generic-function/84046/2 "2022-07-11T11:37:56Z")

</div>

That’s a job for a macro. First see how the function expressions look without and with a varargs argument.

```julia
julia> dump(:(f(; a) = 1))
Expr
  head: Symbol =
  args: Array{Any}((2,))
    1: Expr
      head: Symbol call
      args: Array{Any}((2,))
        1: Symbol f
        2: Expr
          head: Symbol parameters
          args: Array{Any}((1,))
            1: Symbol a
    2: Expr
      head: Symbol block
      args: Array{Any}((2,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[1]
        2: Int64 1

julia> dump(:(f(; a, _...) = 1))
Expr
  head: Symbol =
  args: Array{Any}((2,))
    1: Expr
      head: Symbol call
      args: Array{Any}((2,))
        1: Symbol f
        2: Expr
          head: Symbol parameters
          args: Array{Any}((2,))
            1: Symbol a
            2: Expr # Here is the vararg.
              head: Symbol ...
              args: Array{Any}((1,))
                1: Symbol _
    2: Expr
      head: Symbol block
      args: Array{Any}((2,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[2]
        2: Int64 1

```

Now we need to transform the former to the latter.

```julia
macro ignore_extra_kwargs(ex)
    push!(ex.args[1].args[2].args, :(_...))
    return esc(ex)
end

```

Here it is in action:

```julia
julia> @macroexpand @ignore_extra_kwargs f(; a ) = 1
:(f(; a, _...) = begin
          #= REPL[4]:1 =#
          1
      end)

julia> @ignore_extra_kwargs f(; a ) = 1
f (generic function with 1 method)

julia> f(a = 1, b = 2)
1

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [July 12, 2022, 12:08am UTC](https://discourse.julialang.org/t/add-varargs-to-generic-function/84046/3 "2022-07-12T00:08:54Z")

</div>

Sorry to be stupid. How come this doesn’t work?

`f(; a ) = 1`

`@ignore_extra_kwargs f `

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 12, 2022, 5:59am UTC](https://discourse.julialang.org/t/add-varargs-to-generic-function/84046/4 "2022-07-12T05:59:35Z")

</div>

The macro transforms the code which defines `f`, it doesn’t act on the function.
