# What is interaction between F.() broadcasting and keyword args?

**URL:** https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648
**Category:** General Usage
**Tags:** broadcast
**Created:** [May 11, 2017, 12:12pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648 "2017-05-11T12:12:52Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)
#### Post date: [May 11, 2017, 12:12pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/1 "2017-05-11T12:12:52Z")

</div>

How does F.(a, b; c=d) work with respect to broadcasting? I understand that the arguments a, b will be broadcast. What about the keyword argument?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 11, 2017, 2:40pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/2 "2017-05-11T14:40:37Z")

</div>

`F.(a, b; c=d)` is equivalent to `broadcast((a,b) -> F(a, b; c=d), a, b)`. That is, it does not broadcast over the keyword arguments, but instead passes them inside the closure.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [September 27, 2018, 11:29pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/3 "2018-09-27T23:29:21Z")

</div>

Any workaround to broadcast the keyword arguments? Should I lower these to a `_func`?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 28, 2018, 1:37am UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/4 "2018-09-28T01:37:44Z")

</div>

> [@Nosferican](#):
>
> Any workaround to broadcast the keyword arguments?

Just call `broadcast` explicitly and you can use whatever arguments you want.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [September 28, 2018, 1:41am UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/5 "2018-09-28T01:41:28Z")

</div>

This is suppose to be a friendly end-user API… I ended up lowering the function and checking whether any of the arguments where `AbstractVector` to decide on whether to broadcast or not. Poor man type dispatch since there are too many combinations for a simple type dispatch.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [December 5, 2018, 9:50am UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/6 "2018-12-05T09:50:41Z")

</div>

I came across this thread after encountering a similar situation. The code below seems to offer a feasible solution. Note that there might be a small performance penalty for very simple functions, such as the example below, but it seems to be negligible in most cases.

```julia
f(a,b) = a + b
f(;a,b) = f.(a,b)

```

Output:

```julia
julia> f(;a=1,b=2)
3

julia> f(;a=1,b=[1,2])
2-element Array{Int64,1}:
 2
 3
```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [May 8, 2020, 9:38pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/7 "2020-05-08T21:38:52Z")

</div>

A simple solution is to define a wrapper function:

```julia
wrap(a, b, c) = f(a, b; c=c)
wrap.(A, B, C) # broadcasts on three args.

```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [February 3, 2022, 10:34am UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/8 "2022-02-03T10:34:07Z")

</div>

> [@stevengj](#):
>
> `F.(a, b; c=d)` is equivalent to `broadcast((a,b) -> F(a, b; c=d), a, b)` . That is, it does not broadcast over the keyword arguments, but instead passes them inside the closure.

Is this documented somewhere? In the sense one can rely on this behavior and it won’t change except in 2.0.

I can’t find mention of keyword args in [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 3, 2022, 12:50pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/9 "2022-02-03T12:50:06Z")

</div>

> [@e3c6](#):
>
> Is this documented somewhere? In the sense one can rely on this behavior and it won’t change except in 2.0.

It should be documented; I’ve made a PR: [document behavior of keywords in dot calls by stevengj · Pull Request #44033 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/44033)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [February 3, 2022, 1:59pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/10 "2022-02-03T13:59:27Z")

</div>

Great, thanks.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [September 19, 2022, 7:17am UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/11 "2022-09-19T07:17:43Z")

</div>

```julia
d = 10
x = (;a= 1:3, b=2:4)

f(d;a,b) = d + a * b

```

one way to apply f to the elements of a and b is:

```julia
using DataFramesMeta

@rtransform DataFrame(x) :f = f(d; AsTable(:)... )

```

Ideally I’d like to write

```julia
f.(d; x... )

```

Will this be supported in 2.0 ?  
Is there a better way now? without a function specific wrapper.  
I don’t think broacast works due to the named tuple

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [September 19, 2022, 1:33pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/12 "2022-09-19T13:33:20Z")

</div>

Maybe [DataPipes.jl](https://gitlab.com/aplavin/DataPipes.jl) does what you want?

And if you use StructArrays.jl you can seamlessly convert between named tuple of vectors and vector of names tuples

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [September 20, 2022, 1:55am UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/13 "2022-09-20T01:55:22Z")

</div>

Thanks Peter. I couldn’t find that in StructArrays.jl but found it in SplitApplyCombine.jl. So this works

`@pipe invert(x) .|> f(d; _... )`

Do you know if `f.(d; x... )` will be supported in a later version ?

---

<div class="post-metadata">

### Author: ![charleskawczynski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charleskawczynski/32/13783_2.png) [@charleskawczynski](https://discourse.julialang.org/u/charleskawczynski)
#### Post date: [June 4, 2025, 12:44pm UTC](https://discourse.julialang.org/t/what-is-interaction-between-f-broadcasting-and-keyword-args/3648/14 "2025-06-04T12:44:49Z")

</div>

For future self:

I recently ran into a similar issue with this, but my situation was slightly more complicated in that I needed to splat a NamedTuple into the keywords of a function (because not all of the arguments may exist). In addition, the keys of the NamedTuple needed to be sorted. So, I ended up writing

```julia
@generated function foo(::Val{snames}, ::Val{unames}, vals...) where {snames,unames}
    svals_exprs = []
    for sn in snames
        i = findfirst(un -> un == sn, unames)::Int
        push!(svals_exprs, :(getfield(vals, $i)))
    end
    return quote
        NamedTuple{snames}(($(svals_exprs...),))
    end
end
N = 3;
X = map(_-> (; a=1,b=2), 1:N);
KU = (:b, :a) # unsorted keys
KS = (:a, :b) # sorted keys
A = map(i->2,1:N);
B = map(i->3,1:N);
nt = (;b=B, a=A);
foo.(Val(KS), Val(KU), nt...)
bc = Base.Broadcast.broadcasted(foo, Val(KS), Val(KU), nt...)
Base.materialize(bc)

```
