# Constructor with Varargs

**URL:** https://discourse.julialang.org/t/constructor-with-varargs/84568
**Category:** General Usage
**Tags:** parametersjl
**Created:** [July 21, 2022, 1:56am UTC](https://discourse.julialang.org/t/constructor-with-varargs/84568 "2022-07-21T01:56:26Z")
**Posts on this page:** 3
**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 21, 2022, 1:56am UTC](https://discourse.julialang.org/t/constructor-with-varargs/84568/1 "2022-07-21T01:56:26Z")

</div>

This works

```julia
using Pipe, Parameters

@with_kw struct X
    A
    B
end

x = (A=1, B=2 )
X( x... )

```

but this fails

```julia
x = (A=1, B=2, C=3 )
X( x... )

```

unless you manually filter x to only contain the fields of X.  
or manually create a varargs constructor `X(; A, B, varargs... ) = X( A, B)`

Is there a way around this ?  
Can the X struct automatically create a constructor with Varargs… functionality.  
So that unused variables are dropped without causing an error

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 21, 2022, 2:21am UTC](https://discourse.julialang.org/t/constructor-with-varargs/84568/2 "2022-07-21T02:21:23Z")

</div>

You could create your own macro to do this.

Here is what that macro looks like when expanded.

```julia
julia> @macroexpand @with_kw struct X
           A
           B
       end
quote
    begin
        $(Expr(:meta, :doc))
        struct X
            A
            B
            X(; A = error("Field '" * "A" * "' has no default, supply it with keyword."), B = error("Field '" * "B" * "' has no default, supply it with keyword.")) = begin
                    X(A, B)
                end
            X(A, B) = begin
                    new(A, B)
                end
        end
    end
...
end

```

---

<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 21, 2022, 12:09pm UTC](https://discourse.julialang.org/t/constructor-with-varargs/84568/3 "2022-07-21T12:09:15Z")

</div>

Thanks Mark. looks good.

I was hoping there would be a way for it to happen automatically. But doesn’t seem to be the case
