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:
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 ?