# Why is splatting a non-iteratable allowed?

**URL:** <https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818>\
**Category:** Internals & Design\
**Created:** [July 10, 2020, 2:33am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818 "2020-07-10T02:33:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 10, 2020, 2:33am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/1 "2020-07-10T02:33:52Z")

</div>

Why is this allowed?

```julia
julia> [1.0..., 2.0, 3.0]
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

```

It strikes me as non-intuitive, and has already caused me some bugs.

It’s especially weird when you think about it relative to how it operates relative to other splats:

```julia
julia> [1.0..., 2.0, 3.0]
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

julia> [[1.0]..., 2.0, 3.0]
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

julia> [[[1.0]]..., 2.0, 3.0]
3-element Array{Any,1}:
  [1.0]
 2.0
 3.0

julia> [[[[1.0]]]..., 2.0, 3.0]
3-element Array{Any,1}:
  [[1.0]]
 2.0
 3.0

```

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [July 10, 2020, 2:47am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/2 "2020-07-10T02:47:13Z")

</div>

Numbers are iterable.

It’s a complicated topic, and there have been many discussions about whether or not numbers should be iterable. See e.g. [https://github.com/JuliaLang/julia/issues/7903](https://github.com/JuliaLang/julia/issues/7903), [https://github.com/JuliaLang/julia/pull/19700](https://github.com/JuliaLang/julia/pull/19700), and others.

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 10, 2020, 3:15am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/3 "2020-07-10T03:15:44Z")

</div>

Is there any hope that we can be actually be rid of iterable numbers? It seems like people keep opening issues and PRs to solve this bug, yet it remains.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 10, 2020, 3:18am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/4 "2020-07-10T03:18:04Z")

</div>

Personally, I think it’s nice that numbers are 0 dimensional iterable containers. Makes a lot of sense to me. 🤷‍♂️

```julia
julia> 1[]
1

julia> [i for i in 1]
0-dimensional Array{Int64,0}:
1

julia> collect(1)
0-dimensional Array{Int64,0}:
1

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 10, 2020, 3:18am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/5 "2020-07-10T03:18:07Z")

</div>

The problem is every time someone tries, they become convinced halfway through fixing it that it makes Base code enough uglier that maybe it isn’t such a good idea.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 10, 2020, 3:19am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/6 "2020-07-10T03:19:32Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/4/5/45619df566a8c23b0ca730e67a61c3c1ff8ea28c.png)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 10, 2020, 3:21am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/7 "2020-07-10T03:21:25Z")

</div>

I feel seen.

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 10, 2020, 3:29am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/8 "2020-07-10T03:29:34Z")

</div>

I understand that in some fields there is little distinction between vectors and scalars, but this strikes me as a MATLAB-esque anti-pattern. At the very least it’s a highly unorthodox decision from a PLT perspective. I’m curious what an actual use case of this looks like in Base?

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [July 10, 2020, 3:34am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/9 "2020-07-10T03:34:37Z")

</div>

> [@Samuel\_Ainsworth](#):
>
> I’m curious what an actual use case of this looks like in Base?

Some examples here: [make numbers non-iterable? · Issue #7903 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/7903#issuecomment-269015974)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 10, 2020, 8:56am UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/10 "2020-07-10T08:56:57Z")

</div>

> [@Samuel\_Ainsworth](#):
>
> At the very least it’s a highly unorthodox decision from a PLT perspective.

This is known, and arguably it is not the best decision that was made in designing Julia, but the earliest this can be changed is 2.0 since the change is breaking. And then the benefit in purity may not be worth the breakage and the work. We shall see.

If you really care about this, the best way to convince people is not arguing from a PL design perspective since that is flogging a dead horse, but by making a concrete PR that demonstrates the change.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 10, 2020, 4:11pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/11 "2020-07-10T16:11:17Z")

</div>

I think the [last comment](https://github.com/JuliaLang/julia/issues/7903#issuecomment-624913084) from @stevengj on that thread is particularly on-point:

> My experience in trying to implement even a small piece of this pre-1.0 ([#7903](https://github.com/JuliaLang/julia/issues/7903)) leads me to believe that changing this would lead to a _huge_ amount of code churn over the whole ecosystem. i.e. it wouldn’t be worth it without huge benefits, which I haven’t seen anyone articulate beyond “slightly confusing to some newcomers”.

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 10, 2020, 6:13pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/12 "2020-07-10T18:13:01Z")

</div>

So it sounds like Julia v2.0 is the best shot for cleaning this up. I’d like to avoid dead-horse-beating; this was just my first reaction as a newcomer to the language. I understand every language has its warts, just a matter of improving over time, etc etc.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [July 10, 2020, 7:29pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/13 "2020-07-10T19:29:47Z")

</div>

> [@Samuel\_Ainsworth](#):
>
> I understand every language has its warts

I also find iterable scalars to be odd, but I think you’ll find differing opinions on whether this is a “wart”. Often there is a balance between convenience and purity.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [July 10, 2020, 8:39pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/14 "2020-07-10T20:39:20Z")

</div>

Making numbers iterable does not just decrease “purity”, but also hurts generality and extensibility. As a simple example - there is code that depends on the “dims” argument to various functions to be iterable. This works for `dims=[1, 2, 3]`, works for `dims=1`, but for arrays with named dimensions `dims=:dimension_a` won’t work.  
Basically, this decision just means that numbers are special-cased for little (apparent major) reason.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 10, 2020, 8:50pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/15 "2020-07-10T20:50:44Z")

</div>

I don’t think the derogatory comparison to Matlab is really compelling here. In Matlab, scalars and vectors are 1x1 and nx1 matrices respectively. In julia we have a quite powerful type system and multiple dispatch where scalars, vectors, covectors, matrices and higher dimensional structures are all distinct and distinguishable at compile time. Numbers are not arrays and arrays do not have to contain numbers.

I guess what I’d have to say about things like indexing, iterating or splatting numbers is: what else would it mean than what it means now?

I think it’s a fairly unambiguous behaviour, even if it is a little surprising to new users.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 10, 2020, 8:52pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/16 "2020-07-10T20:52:42Z")

</div>

> [@Mason](#):
>
> I guess what I’d have to say about things like indexing, iterating or splatting numbers is: what else would it mean than what it means now?

An error! I think there’s good reason to make this change, but I’m firmly in the camp of it being too much churn for not enough benefit. I’d be very happy to be convinced otherwise, though.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 10, 2020, 9:02pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/17 "2020-07-10T21:02:02Z")

</div>

How does this relate to the concept of empty sums or empty products, if at all?

> **[Empty sum](https://en.wikipedia.org/wiki/Empty_sum)**
>
> In mathematics, an empty sum, or nullary sum, is a summation where the number of terms is zero. 
> The natural way to extend non-empty sums is to let the empty sum be the additive identity.
> Let 
>   
>     
>       
>         
> a
>           
> 1
>           
>         
>       
>     
> {\\displaystyle a\_{1}}
>   
> , 
>   
>     
>       
>         
> a
>           
> 2
>           
>         
>       
>     
> {\\displaystyle a\_{2}}
>   
> , 
>   
>     
>       
>         
> a
>           
> ...

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 10, 2020, 9:19pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/18 "2020-07-10T21:19:42Z")

</div>

> [@mbauman](#):
>
> An error! I think there’s good reason to make this change, but I’m firmly in the camp of it being too much churn for not enough benefit. I’d be very happy to be convinced otherwise, though.

I guess I’d just say that we should save errors for when there’s not a clear meaning of something. Hence, I think it makes sense to throw an error if I do

```julia
struct Foo end
[Foo()...]

```

because we don’t know what sort of thing the programmer intended `Foo` to be.

On the other hand, its pretty uncontroversial to consider `Number`s to be scalars. To me, if I asked “how many dimensions does a scalar have?” The answer is “0”, not “undefined”.

For me, once I can accept that numbers have a well defined number of dimensions or axes (I.e. 0), then it seems that the indexing and iterating behaviour just comes along for the ride because we already went through all the work elsewhere of defining the behaviour of zero dimensional objects.

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 10, 2020, 9:22pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/19 "2020-07-10T21:22:47Z")

</div>

> [@Mason](#):
>
> On the other hand, its pretty uncontroversial to consider `Number` s to be scalars. To me, if I asked “how many dimensions does a scalar have?” The answer is “0”, not “undefined”.

But the issue here is that the effective dimensionality of a number in Julia is currently 1, not 0:

```julia
julia> length([5...])
1
julia> length(5)
1

```

But even then, I don’t think the idea of a non-container type having a length makes all that much sense…

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 10, 2020, 9:25pm UTC](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818/20 "2020-07-10T21:25:49Z")

</div>

Length is not dimensionality.

```julia
julia> ndims(5)
0

julia> size(5)
()

julia> axes(5)
()

```

It has zero dimensions and one element, so the length is one.

[Next page](https://discourse.julialang.org/t/why-is-splatting-a-non-iteratable-allowed/42818.md?page=2)
