How to ignore unnecessary function arguments?

# Hello, i have any structures with kwargs, like this
Base.@kwdef struct Foo
  a
  b
  c
end

# and i have any named tuples like this
nt = (a=1, b=2, c=3, d=4, e=5, f=6, g=7)  # ...

# always :
issubset(fieldnames(Foo), keys(nt ))  # -> true

# is there a universal way to ignore unnecessary arguments???
Foo(; nt...)  # -> ERROR: MethodError: ...
1 Like

By universal, are you trying to match the parameter names of a given struct to those of a named tuple? And have an instance be built based on indexing the named tuple for the same parameter names?

Two quick solutions come to mind:

Foo(nt[:a], [:b], [:c])

And this generic one.

foo_vals = filter(x -> in(x, fieldnames(Foo)), keys(nt))
Foo(nt[foo_vals]...)
2 Likes

I’d just write

Foo(;a, b, c, kwargs...) = Foo(a, b, c)

that way, Foo(; nt...) works.

4 Likes