# Function select only compatible args

**URL:** <https://discourse.julialang.org/t/function-select-only-compatible-args/87207>\
**Category:** Internals & Design\
**Created:** [September 13, 2022, 11:03am UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207 "2022-09-13T11:03:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 13, 2022, 11:03am UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/1 "2022-09-13T11:03:22Z")

</div>

In my opinion it would be nice if this could be done directly, :

```julia
f1(a1; a2=1, a3="a") # a4, a5, ...
f2(b1; b2='k', b3=2.0) # b4, b5, ...

function f(x, y, z, a1, b1; kwargs...)
    # code
    f1(a1; kwargs...) 
    # code
    f2(b1; kwargs...)
    #code
end

```

where f1 and f2 have different kwargs, and each function automatically chooses only compatible kwargs.  
Wrapping of functions would be greatly simplified.  
Currently doing something like this is a bit complicated, and it has to be done by hand for each function called in f.  
What do you think about it?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 13, 2022, 11:07am UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/2 "2022-09-13T11:07:27Z")

</div>

You can try this:

```julia
f1(a1; a2=1, a3="a", kw...) = println("a2: ", a2, ", a3: ", a3)
f2(b1; b2='k', b3=2.0, kw...) = println("b2: ", b2, ", b3: ", b3)

function f(x, y, z, a1, b1; kwargs...)
    # code
    f1(a1; kwargs...) 
    # code
    f2(b1; kwargs...)
    #code
end

```

`f1` and `f2` slurp and discard unused args in `kw`, so they actually _do_ automatically choose only compatible kwargs now.

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 13, 2022, 11:09am UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/3 "2022-09-13T11:09:52Z")

</div>

f1 and f2 come from a Pkg.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 13, 2022, 11:15am UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/4 "2022-09-13T11:15:50Z")

</div>

I don’t have an answer in that case. There is a related discussion here: [Finding possible keyword arguments](https://discourse.julialang.org/t/finding-possible-keyword-arguments/40752/1)  
(I messed up the link at first. Scroll to the top.)

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 13, 2022, 3:50pm UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/5 "2022-09-13T15:50:10Z")

</div>

It’s not pretty, but perhaps this will work:

```julia
f1(a1; a2=1, a3="a") # a4, a5, ...
f2(b1; b2='k', b3=2.0) # b4, b5, ...

function f(x, y, z, a1, b1; kwargs...)
    # code
    f1(a1; a2=get(kwargs,:a2,1), a3=get(kwargs,:a3,"a")) 
    # code
    f2(b1; b2=get(kwargs,:b2,'k'), b3=get(kwargs,:b3,2.0))
    #code
end

```

There are cleaner ways that would involve subselecting your initial `kwargs` to produce ones that only have the desired subset of values for each call, but this could work.

Alternatively, you could do something like this

```julia
function f(x, y, z, a1, b1; f1kw = NamedTuple(), f2kw=NamedTuple())
    # code
    f1(a1; f1kw...) 
    # code
    f2(b1; f2kw...)
    #code
end

f(1, 2, 3, 4, 5; f1kw=(a2=6,), f2kw=(b3=2.5,b2='j'))

```

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 14, 2022, 8:12pm UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/6 "2022-09-14T20:12:30Z")

</div>

@DNF  
Can adding kwargs… to all the functions I’ve written cause performance issues?  
In general, is it a good practice to write functions that accept kwargs which are then not used?

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 14, 2022, 8:19pm UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/7 "2022-09-14T20:19:02Z")

</div>

Could someone give me an example of how I can write a macro that takes the function and passes to the called functions only compatible named arguments?

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [September 15, 2022, 12:42pm UTC](https://discourse.julialang.org/t/function-select-only-compatible-args/87207/8 "2022-09-15T12:42:41Z")

</div>

You could also try using `select` from [NamedTupleTools.jl](https://github.com/JeffreySarnoff/NamedTupleTools.jl#select). Something like:

```julia
function f(x, y, z, a1, b1; kwargs...)
    f1(a1; select(NamedTuple(kwargs), (:a2, :a3))...)
    f2(b1; select(NamedTuple(kwargs), (:b2, :b3))...)
end

```

That might be easier if you have to forward a lot of keyword arguments.
