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

broadcast

#1

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?


#2

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.


#3

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


#4

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


#5

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.


#6

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.

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

Output:

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

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