# Numbers as single-element collections

**URL:** <https://discourse.julialang.org/t/numbers-as-single-element-collections/1621>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [January 20, 2017, 7:09pm UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621 "2017-01-20T19:09:38Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [January 20, 2017, 7:09pm UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621/1 "2017-01-20T19:09:39Z")

</div>

Hi, I was reading `base/number.jl`, and I was wondering about the rationale of

```julia
start(x::Number) = false
next(x::Number, state) = (x, true)
done(x::Number, state) = state
isempty(x::Number) = false
in(x::Number, y::Number) = x == y

```

which makes numbers single-element collections.

1. Where could I learn more about the rationale?
2. Why restrict to numbers?

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [January 20, 2017, 7:33pm UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621/2 "2017-01-20T19:33:53Z")

</div>

There has been some discussion at [https://github.com/JuliaLang/julia/pull/19700](https://github.com/JuliaLang/julia/pull/19700) and issues linked in it.

---

<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:** [January 25, 2017, 7:11pm UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621/3 "2017-01-25T19:11:55Z")

</div>

Basically, it’s often convenient to consider scalar numbers to be immutable zero-dimensional arrays containing only themselves. In fact, Matlab doesn’t distinguish between scalars, single-element vectors or single-element matrices, at all – they’re all represented as 1×1 matrices. However, that can occasionally cause hard-to-spot bugs, the most common being writing `for i = n` when one means `for i = 1:n`.

---

<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:** [January 26, 2017, 9:19am UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621/4 "2017-01-26T09:19:30Z")

</div>

R does the same (has no scalars). IMO for Julia, it is one of those DWIM constructs that look nice initially, but are more trouble than what they are worth. AFAIK it is not documented (at least in `?in`), and then it special-cases numbers.

Anyway, it is not a big deal, and maybe when I read more code in `Base` I will understand the reasons (or submit a PR 😉)

---

<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:** [January 26, 2017, 2:00pm UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621/5 "2017-01-26T14:00:30Z")

</div>

I’ve been pushing for the removal of this feature since 2010, so believe me, the discussion has been had. The trouble is that every time someone tries to do it, it turns out to be surprisingly painful – there’s a remarkably large amount of code even in the standard library that seems to rely on this feature. A PR to remove it would be welcomed, however.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [January 27, 2017, 10:48am UTC](https://discourse.julialang.org/t/numbers-as-single-element-collections/1621/6 "2017-01-27T10:48:20Z")

</div>

The new broadcast semantic for v0.6 (scalar by default) should help, and removing these methods would be consistent with that.

Agreed that it’s tons of work, though.
