# Save kwargs in struct field to be used later in function calls

**URL:** https://discourse.julialang.org/t/save-kwargs-in-struct-field-to-be-used-later-in-function-calls/34376
**Category:** General Usage
**Tags:** struct, kwargs
**Created:** [February 9, 2020, 1:04pm UTC](https://discourse.julialang.org/t/save-kwargs-in-struct-field-to-be-used-later-in-function-calls/34376 "2020-02-09T13:04:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [February 9, 2020, 1:04pm UTC](https://discourse.julialang.org/t/save-kwargs-in-struct-field-to-be-used-later-in-function-calls/34376/1 "2020-02-09T13:04:04Z")

</div>

I have a struct `Foo` with a field that contains keyword arguments given at construction. I want to then be able to call a function with keyword arguments recovered from Foo:

```julia
struct Foo
    kwargs
end

Foo(;kwargs...) = Foo(kwargs)

f(; kwargs...) = nothing

g(foo::Foo) = f(foo.kwargs)

julia> foo = Foo(a = 1, b = 2)
Foo(Base.Iterators.Pairs(:a => 1,:b => 2))

julia> g(foo)
ERROR: MethodError: no method matching f(::Base.Iterators.Pairs{Symbol,Int64,Tuple{Symbol,Symbol},NamedTuple{(:a, :b),Tuple{Int64,Int64}}})
Closest candidates are:
  f(; kwargs...) at none:1
Stacktrace:
 [1] g(::Foo) at .\none:1
 [2] top-level scope at none:0

```

I have tried different approaches but none worked. Notably using `Foo(;kwargs...) = Foo(values(kwargs))` to save a NamedTuple instead of a a Iterators.pairs. Is there some way to make this work or am I trying to do something that doesn’t make sense ?

---

<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: [February 9, 2020, 1:07pm UTC](https://discourse.julialang.org/t/save-kwargs-in-struct-field-to-be-used-later-in-function-calls/34376/2 "2020-02-09T13:07:55Z")

</div>

you want to splice it in as

```julia
f(; foo.kwargs...)

```

(incidentally, see the “closest candidates” error message).

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [February 9, 2020, 1:13pm UTC](https://discourse.julialang.org/t/save-kwargs-in-struct-field-to-be-used-later-in-function-calls/34376/3 "2020-02-09T13:13:44Z")

</div>

Ha okay, what I had tried in that same idea was

```julia
julia> g(foo::Foo) = f(foo.kwargs...)
g (generic function with 3 methods)

julia> g(foo)
ERROR: MethodError: no method matching f(::Pair{Symbol,Int64}, ::Pair{Symbol,Int64})
Stacktrace:
 [1] g(::Foo) at .\none:1
 [2] top-level scope at none:0

```

without the `;`. I did not know that the semi colon was used in funcion calls, I thought it was only in definitions. But yes it makes sense since it is trying to use the Iterator as varargs. Thank you.

---

<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: [February 9, 2020, 1:48pm UTC](https://discourse.julialang.org/t/save-kwargs-in-struct-field-to-be-used-later-in-function-calls/34376/4 "2020-02-09T13:48:07Z")

</div>

The role of `;` is to separate the keyword and the positional arguments. Splatting keyword arguments is documented in this section:

[https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments-1](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments-1)
