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

I have the following

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?

The collect is introduced as part of lowering:

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))
5 Likes

Thank you for your very quick response.