Sharing keyword args between functions

This is a style question, I already have a solution, just looking for opinions.

Sometimes I find it convenient to specify defaults with keyword arguments for a couple of functions, which are then invoked by some other function, where I don’t want to repeat the defaults. So I collect the keywords args with ..., and pass them on. But I need to collect the unused ones in the functions that are called; anticipating #9343 I am doing it like this (MWE):

pick_a(; a = 5, _...) = a
pick_b(; b = 7, _...) = b
ab(; args...) = pick_a(; args...) + pick_b(; args...)
ab()                            # 12
ab(; a = 1)                     # 8

Is this an OK interface design? Are there any caveats?

(I know that for anything more serious I should wrap parameters up in a struct, but that is sometimes too involved).

2 Likes

Looks fine as long as it doesn’t get too messy. (Doesn’t sound too useful for simple pick_* but could be useful when there are multiple parts that each do something with part of the kws).

Do note that this currently come with a high performance cost. Once the performance cost is fixed though, this will likely be no different from wrap parameters up in a struct.

I am using this for high-level logic so it should not matter, but I am curious about it; can you tell me which issues/PRs I should read up on and follow?

https://github.com/JuliaLang/julia/pull/22194
https://github.com/JuliaLang/julia/pull/21915
https://github.com/JuliaLang/julia/pull/16580

1 Like