# Document splatting behaviour in broadcasting

**URL:** https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068
**Category:** General Usage
**Tags:** documentation, broadcasting
**Created:** [November 25, 2021, 3:24pm UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068 "2021-11-25T15:24:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)
#### Post date: [November 25, 2021, 3:24pm UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/1 "2021-11-25T15:24:00Z")

</div>

Hello!

This use of splatting just turned up in my code:

```julia
# using Pkg
# Pkg.add("ColorTypes")
using ColorTypes

_colors = [[0.0,0.2,1.0], [0.0,0.5,0.0], [1.0,0.0,0.0]];
_color = RGB.(_colors...)
# 3-element Array{RGB{Float64},1} with eltype RGB{Float64}:
# RGB{Float64}(0.0,0.0,1.0)
# RGB{Float64}(0.2,0.5,0.0)
# RGB{Float64}(1.0,0.0,0.0)

```

Here, splatting is applied somehow “before” broadcasting, in the sense that it splats each of `_colors` subarrays, and then broadcasts over each component.

However, what I might have expected is the output produced by this:

```julia
_color = [RGB(a...) for a in _colors]
# 3-element Array{RGB{Float64},1} with eltype RGB{Float64}:
# RGB{Float64}(0.0,0.2,1.0)
# RGB{Float64}(0.0,0.5,0.0)
# RGB{Float64}(1.0,0.0,0.0)

```

Is this behavior documented anywhere?

Thanks 🙂

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [November 25, 2021, 4:01pm UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/2 "2021-11-25T16:01:01Z")

</div>

I think this behaviour feels natural to me at least, the splatting is “further in” into the expression and evaluated before is my intuition.

Looking [here](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting) we see that doing `f.(a,b)` is pretty much the same as `broadcast(f, a, b)`, meaning that your example with `RGB.(_colors...)` would be the same as calling `broadcast(RGB, _colors...)` where it is reasonable that the splatting should happen before entering the broadcast function.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 25, 2021, 4:14pm UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/3 "2021-11-25T16:14:35Z")

</div>

In order to get the splatting at the outer level, you need to broadcast a function that takes one argument, and splats it into a call to RGB. This is what `Base.splat` does for you (although I’m not sure whether it is part of the public API).

```julia
julia> using ColorTypes

julia> _colors = [[0.0,0.2,1.0], [0.0,0.5,0.0], [1.0,0.0,0.0]];

julia> _color = RGB.(_colors...)
3-element Array{RGB{Float64},1} with eltype RGB{Float64}:
 RGB{Float64}(0.0,0.0,1.0)
 RGB{Float64}(0.2,0.5,0.0)
 RGB{Float64}(1.0,0.0,0.0)

julia> _color = Base.splat(RGB).(_colors)
3-element Array{RGB{Float64},1} with eltype RGB{Float64}:
 RGB{Float64}(0.0,0.2,1.0)
 RGB{Float64}(0.0,0.5,0.0)
 RGB{Float64}(1.0,0.0,0.0)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 25, 2021, 5:00pm UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/4 "2021-11-25T17:00:59Z")

</div>

@ffevotte’s solution is awesome.

Still not very clear why the OP’s example broadcasts as shown, but just to add that the desired result can be achieved with a little more splatting work:

```julia
RGB.(eachrow(hcat(_colors...))...)

 RGB{Float64}(0.0,0.2,1.0)
 RGB{Float64}(0.0,0.5,0.0)
 RGB{Float64}(1.0,0.0,0.0)

```

---

<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: [November 25, 2021, 6:39pm UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/5 "2021-11-25T18:39:17Z")

</div>

> [@nandoconde](#):
>
> Here, splatting is applied somehow “before” broadcasting

But that’s what parentheses _do_, right? They group things and enforce precedence. In

```julia
a * (b + c) 

```

won’t you expect the contents of the parentheses to execute first?

BTW, this

> [@nandoconde](#):
>
> `_colors = [[0.0,0.2,1.0], [0.0,0.5,0.0], [1.0,0.0,0.0]];`

should be a tuple:

```julia
_colors = ([0.0,0.2,1.0], [0.0,0.5,0.0], [1.0,0.0,0.0]);

```

Splatting vectors is not great, and `RGB` takes three arguments, so a 3-tuple is exactly the right concept.

---

<div class="post-metadata">

### Author: ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)
#### Post date: [November 26, 2021, 7:38am UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/7 "2021-11-26T07:38:39Z")

</div>

> [@DNF](#):
>
> Splatting vectors is not great, and `RGB` takes three arguments, so a 3-tuple is exactly the right concept.

I do not care much about the whole tuple/vector since this was a MWE.

But it was better to have an array of arrays so that the output was always an `Array` of `RGB` , and thus more visually consistent.

> [@ffevotte](#):
>
> In order to get the splatting at the outer level, you need to broadcast a function that takes one argument, and splats it into a call to RGB. This is what `Base.splat` does for you (although I’m not sure whether it is part of the public API).

Okay, this was amazing. I had absolutely no idea that this existed, and it’s awesome. Thanks!

Still, wouldn’t it be better that this behavior of `...` was documented somewhere?

---

<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: [November 26, 2021, 7:53am UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/8 "2021-11-26T07:53:08Z")

</div>

> [@nandoconde](#):
>
> But it was better to have an array of arrays so that the output was always an `Array` of `RGB` , and thus more visually consistent.

No, the output would be the same, only the performance is better, and it makes more sense.

> [@nandoconde](#):
>
> since this was a MWE.

I don’t really agree with the ‘just an MWE’ way of thinking. I always assume that in an MWE, almost everything is significant, or it wouldn’t be ‘minimal’. Also, the fact that you misunderstand what the output would be, tells me that it was relevant information.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 26, 2021, 8:00am UTC](https://discourse.julialang.org/t/document-splatting-behaviour-in-broadcasting/72068/9 "2021-11-26T08:00:03Z")

</div>

> [@nandoconde](#):
>
> Still, wouldn’t it be better that this behavior of `...` was documented somewhere?

I guess it’s not so much the behavior of `...` that surprises you, but rather whether it applies to the call to `RGB` itself (what you’d have expected) or the broadcast as a whole (what actually happens).

As pointed out by @albheim earlier in this thread, there _is_ a sentence explaining what goes on in the [reference documentation for broadcast](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting):

> In fact, `f.(args...)` is equivalent to `broadcast(f, args...)`

Still, your being surprised by this behavior might be a good indication that this is not sufficient, and you’re probably in the best position to help improve the situation by suggesting which parts of the documentation need fixing, and how they might be made clearer.
