# How does Julia go from array comprehension to collect(::Generator)?

**URL:** <https://discourse.julialang.org/t/how-does-julia-go-from-array-comprehension-to-collect-generator/9705>\
**Category:** General Usage\
**Created:** [March 14, 2018, 1:20am UTC](https://discourse.julialang.org/t/how-does-julia-go-from-array-comprehension-to-collect-generator/9705 "2018-03-14T01:20:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [March 14, 2018, 1:20am UTC](https://discourse.julialang.org/t/how-does-julia-go-from-array-comprehension-to-collect-generator/9705/1 "2018-03-14T01:20:19Z")

</div>

I have the following

```julia
julia> Base.collect(::Base.Generator{MyCollection}) = print("success")
julia> [x for x in MyCollection([1,2])]
success

```

I can’t figure out how Julia goes from parsing the comprehension `[x for x in MyCollection([1,2])]` to calling `Base.collect(::Base.Generator{MyCollection}) `. Is this done via an undocumented operator with a special name, just like [A B C] results in a call to Julia function `hcat`? In other words, does `[x for x in ...]` directly result in a call to `Base.collect(::Base.Generator)` or are there a few steps in between?

Any suggestions?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [March 14, 2018, 1:46am UTC](https://discourse.julialang.org/t/how-does-julia-go-from-array-comprehension-to-collect-generator/9705/2 "2018-03-14T01:46:00Z")

</div>

The collect is introduced as part of lowering:

```julia
julia> f() = [x for x in MyCollection([1,2])]
f (generic function with 2 methods)

julia> @code_lowered f()
CodeInfo(:(begin
      nothing
      #11 = $(Expr(:new, :(Main.:(##11#12))))
      Core.SSAValue(0) = #11
      Core.SSAValue(1) = (Base.vect)(1, 2)
      Core.SSAValue(2) = (Main.MyCollection)(Core.SSAValue(1))
      Core.SSAValue(3) = (Base.Generator)(Core.SSAValue(0), Core.SSAValue(2))
      Core.SSAValue(4) = (Base.collect)(Core.SSAValue(3))
      return Core.SSAValue(4)
  end))

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [March 14, 2018, 1:51am UTC](https://discourse.julialang.org/t/how-does-julia-go-from-array-comprehension-to-collect-generator/9705/3 "2018-03-14T01:51:37Z")

</div>

Thank you for your very quick response.
