# Unclear documentation of \`collect\`? (similarity and difference with brackets)

**URL:** https://discourse.julialang.org/t/unclear-documentation-of-collect-similarity-and-difference-with-brackets/101843
**Category:** General Usage
**Tags:** documentation
**Created:** [July 20, 2023, 3:33pm UTC](https://discourse.julialang.org/t/unclear-documentation-of-collect-similarity-and-difference-with-brackets/101843 "2023-07-20T15:33:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)
#### Post date: [July 20, 2023, 3:33pm UTC](https://discourse.julialang.org/t/unclear-documentation-of-collect-similarity-and-difference-with-brackets/101843/1 "2023-07-20T15:33:20Z")

</div>

Hello,

I just came across the docstring of `collect` ([Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v1/base/collections/#Base.collect-Tuple%7BAny%7D)) and I’ve got the feeling that the second usage example would need some more explanation, or is it just me?

```julia
julia> [x^2 for x in 1:8 if isodd(x)]
4-element Vector{Int64}:
  1
  9
 25
 49

```

(the first example is more simply `collect(1:2:13)`)

The way I understand this example is that _“if you put a Generator inside brackets, you get the same result as collect(…)'ing that generator”_. Is my interpretation correct?

Also, if my interpreation is correct, I believe that mentioning the `[...]` notation/operator would require an additional warning, because the similarity is partial:

- indeed `[i for i in 1:3]` and `collect(i for i in 1:3)` both returns the same 3-element `Vector{Int64}`
- however, `[1:3]` returns a 1-element `Vector{UnitRange{Int64}}`, while `collect(1:3)` returns the same 3-element `Vector{Int64}` as before

---

<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: [July 20, 2023, 4:45pm UTC](https://discourse.julialang.org/t/unclear-documentation-of-collect-similarity-and-difference-with-brackets/101843/2 "2023-07-20T16:45:52Z")

</div>

The bracket generator syntax in your example more or less ends up lowering to this:

```julia
f(x) = x^2
itr = 1:8
filterfunc(x) = isodd(x)
gen = Base.Generator(f, Base.Filter(filterfunc, itr))
collect(gen)

```

You can take a look at the exact lowered code with `Meta.@lower`.

So yes, it is _exactly_ the same as calling `collect` on a `Base.Generator`.

> [@pierre-haessig](#):
>
> however, `[1:3]` returns a 1-element `Vector{UnitRange{Int64}}`, while `collect(1:3)` returns the same 3-element `Vector{Int64}` as before

Only bracket expressions with a `for` in them are generator expressions - without them, they are just syntax for array literals.

---

<div class="post-metadata">

### Author: ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)
#### Post date: [July 21, 2023, 9:10am UTC](https://discourse.julialang.org/t/unclear-documentation-of-collect-similarity-and-difference-with-brackets/101843/3 "2023-07-21T09:10:25Z")

</div>

Thanks for the explanation, but my question was more about the clarity of the documentation (I’ve updated the topic title to emphasize this).

I’ve now created the [Pull request #50619](https://github.com/JuliaLang/julia/pull/50619) with a proposition to expand the docstring of `collect`.
