# 'for loop' and Nothing

**URL:** <https://discourse.julialang.org/t/for-loop-and-nothing/79367>\
**Category:** New to Julia\
**Created:** [April 12, 2022, 7:21am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367 "2022-04-12T07:21:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anikaiful](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anikaiful/32/35024_2.png) [@anikaiful](https://discourse.julialang.org/u/anikaiful)\
**Post date:** [April 12, 2022, 7:21am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/1 "2022-04-12T07:21:51Z")

</div>

Nothing (pun intended) “surprised” me a little bit…

```julia
   foo = nothing
   for x in foo
       println("should not see this")
   end

```

Instead of silently skipping the whole `for x` block, I got  
`ERROR: MethodError: no method matching iterate(::Nothing)`. Nothing ( xD ) I can’t work with, but I (hoped? expected?) ‘for’-loop’d treat Nothing as an empty/zero iterator.

P.S.: this isn’t a question, just a brief observation.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 12, 2022, 7:34am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/2 "2022-04-12T07:34:13Z")

</div>

The error makes sense to me - `nothing` can be seen here as “no thing” to iterate over, which is not the same as an empty collection. Having it iterable would imo only lead to more confusing behavior like

```julia
for x in 1
   println(x) # prints 1!
end

```

---

<div class="post-metadata">

**Author:** ![anikaiful](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anikaiful/32/35024_2.png) [@anikaiful](https://discourse.julialang.org/u/anikaiful)\
**Post date:** [April 12, 2022, 7:40am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/3 "2022-04-12T07:40:26Z")

</div>

Oh, aye, that’d be weird indeed. I certainly can live with how it is right now anyway - was just tinkering with a thought of using e.g. `Union{Nothing, Vector{...}}` style at a few parts of code (but there’s simpler ways in existence than messing with that beehive, I know).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 12, 2022, 7:58am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/4 "2022-04-12T07:58:33Z")

</div>

> [@anikaiful](#):
>
> was just tinkering with a thought of using e.g. `Union{Nothing, Vector{...}}` style at a few parts of code

Yeah, that’s its intended use! You can use `something` to access e.g. a field with that type. Sometimes I wish it’d only unpack `Some`, but alas, that’s not the world we live in and changing that behavior is breaking.

---

<div class="post-metadata">

**Author:** ![anikaiful](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anikaiful/32/35024_2.png) [@anikaiful](https://discourse.julialang.org/u/anikaiful)\
**Post date:** [April 12, 2022, 9:29am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/5 "2022-04-12T09:29:57Z")

</div>

`something`… that’s handy. A bugger to try google tho.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [April 12, 2022, 10:12am UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/6 "2022-04-12T10:12:24Z")

</div>

Note also that you’re iterating the type `Nothing` here, while `nothing` is the corresponding singleton value. Although your error message fits iterating `nothing`, so maybe it was just a copy error.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 12, 2022, 12:14pm UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/7 "2022-04-12T12:14:33Z")

</div>

> [@anikaiful](#):
>
> `something` … that’s handy. A bugger to try google tho.

You can query docstrings from the REPL, like `julia>?something` which enters [help mode](https://docs.julialang.org/en/v1/stdlib/REPL/#Help-mode) and displays any docstrings associated with the given symbol.

---

<div class="post-metadata">

**Author:** ![anikaiful](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anikaiful/32/35024_2.png) [@anikaiful](https://discourse.julialang.org/u/anikaiful)\
**Post date:** [April 12, 2022, 12:36pm UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/8 "2022-04-12T12:36:17Z")

</div>

> [@jules](#):
>
> Note also that you’re iterating the type `Nothing` here, while `nothing` is the corresponding singleton value. Although your error message fits iterating `nothing`, so maybe it was just a copy error.

Yeah, typo in post, `foo = Nothing` oughta read `foo = nothing`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 12, 2022, 1:59pm UTC](https://discourse.julialang.org/t/for-loop-and-nothing/79367/9 "2022-04-12T13:59:06Z")

</div>

> [@Sukera](#):
>
> ```julia
> for x in 1
> println(x) # prints 1!
> end
> 
> ```

because of this actually I was expecting that `for x in nothing` printed `nothing`.
