# Why are scalars iterable when broadcasting would still work fine if they weren't?

**URL:** <https://discourse.julialang.org/t/why-are-scalars-iterable-when-broadcasting-would-still-work-fine-if-they-werent/84785>\
**Category:** Internals & Design\
**Tags:** question, offtopic\
**Created:** [July 26, 2022, 3:15am UTC](https://discourse.julialang.org/t/why-are-scalars-iterable-when-broadcasting-would-still-work-fine-if-they-werent/84785 "2022-07-26T03:15:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [July 26, 2022, 3:15am UTC](https://discourse.julialang.org/t/why-are-scalars-iterable-when-broadcasting-would-still-work-fine-if-they-werent/84785/1 "2022-07-26T03:15:43Z")

</div>

This is a soft-question about an aspect of Julia’s design and history.

It’s a [well-discussed](https://github.com/JuliaLang/julia/issues/7903) point that scalars are iterable, in the sense that:

```julia
julia> 42[1], 42[], size(42), length(42)
(42, 42, (), 1)

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

```

I previously thought that a primary reason for this design is that it allows broadcasting expressions such as `10 .* (1:5)`. However, the broadcasting interface is nice enough to “just work” even if scalars didn’t satisfy the array interface. Take `Symbol`s, for example, which aren’t iterable:

```julia
julia> ++(a, b) = (a, b)
++ (generic function with 1 method)

julia> :not_iterable .++ (1:3) # broadcasting "just works"
3-element Array{Tuple{Symbol,Int64},1}:
 (:not_iterable, 1)
 (:not_iterable, 2)
 (:not_iterable, 3)

```

This works because `Broadcast.broadcastable(:symbol) == Ref(:symbol)`.

Is there some other reason that iterability of scalars is useful or convenient?

I understand that removing this feature would without good reason would be too disruptive. Rather, I’m curious about the history of this design decision. **What were the original reasons / would the same design decision be made now?**

---

<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 26, 2022, 3:22am UTC](https://discourse.julialang.org/t/why-are-scalars-iterable-when-broadcasting-would-still-work-fine-if-they-werent/84785/2 "2022-07-26T03:22:36Z")

</div>

Simple answer: `Broadcast.broadcastable` was implemented quite recently as compared to the iterable-ness of scalars — I believe it was even more recently than their most recent excision attempt.

---

<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:** [July 26, 2022, 3:24am UTC](https://discourse.julialang.org/t/why-are-scalars-iterable-when-broadcasting-would-still-work-fine-if-they-werent/84785/3 "2022-07-26T03:24:11Z")

</div>

See [Map vs list comprehension - #9 by stevengj](https://discourse.julialang.org/t/map-vs-list-comprehension/916/9)
