Splat ComponentArray into keyword arguments

Hi all,

I have a situation in which “splatting” a ComponentArray into keyword arguments would be useful. Unfortunately, it is not currently possible. I was wondering whether it would be possible in principle? Could I add that capability?

MWE

using ComponentArrays

x = ComponentArray(a = 1, b = 2)

f(;a, b) = a + b

f(;x...)

That might be a good thing to add. It looks like it may only need a merge method defined. Would you mind opening up an issue for that?

In the meantime, though, you could always do something like:

julia> f(; NamedTuple(x)...)
3
1 Like

Awesome. I will add an issue. Thanks!

1 Like

Although converting to a NamedTuple does work, it does add some overhead. Out of curiosity, would your more permanent solution with merge be more efficient?

Unfortunately merge is going to have to convert to NamedTuple to perform the merge with the empty NamedTuple anyway (which is how keyword argument splatting works). I don’t think there is any way to buy into the keyword arg splatting without doing this. Maybe I’m wrong, though. Might be worth checking.

1 Like