# Why are the inferred types different between collect and comprehension?

**URL:** <https://discourse.julialang.org/t/why-are-the-inferred-types-different-between-collect-and-comprehension/92590>\
**Category:** General Usage\
**Tags:** inference, type-stability\
**Created:** [January 6, 2023, 6:41am UTC](https://discourse.julialang.org/t/why-are-the-inferred-types-different-between-collect-and-comprehension/92590 "2023-01-06T06:41:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 6, 2023, 6:41am UTC](https://discourse.julialang.org/t/why-are-the-inferred-types-different-between-collect-and-comprehension/92590/1 "2023-01-06T06:41:08Z")

</div>

```julia
julia> using ApproxFun

julia> A = Derivative(); B = Multiplication(Fun());

julia> ops = (A, B);

julia> [ops...] |> eltype
Operator{Float64}

julia> collect(ops) |> eltype
Operator

```

Shouldn’t these lead to the same result?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [January 6, 2023, 7:16am UTC](https://discourse.julialang.org/t/why-are-the-inferred-types-different-between-collect-and-comprehension/92590/2 "2023-01-06T07:16:47Z")

</div>

Note that that is not a comprehension.

The `[]` array constructor promotes argument types, but `collect` and the comprehension evidently do not:

```julia
julia> [1, 1.0, 1f0]
3-element Vector{Float64}:
 1.0
 1.0
 1.0

julia> collect((1, 1.0, 1f0))
3-element Vector{Real}:
 1
 1.0
 1.0f0

julia> [x for x in ((1, 1.0, 1f0))]
3-element Vector{Real}:
 1
 1.0
 1.0f0

```

Look at the lowered code for `[1, 1.0, 1f0]`:

```julia
julia> @code_lowered [1, 1.0, 1f0]
CodeInfo(
1 ─ T = Core._apply_iterate(Base.iterate, Base.promote_typeof, X)
│ %2 = X
│ %3 = Core.tuple(T)
│ %4 = Core._apply_iterate(Base.iterate, Base.getindex, %3, %2)
└── return %4
)

```

Note that it is basically `Base.promote_typeof(1, 1.0, 1f0)[1, 1.0, 1f0]`, i.e. `Float64[1, 1.0, 1f0]` thanks to that `promote_typeof`.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 6, 2023, 9:05am UTC](https://discourse.julialang.org/t/why-are-the-inferred-types-different-between-collect-and-comprehension/92590/3 "2023-01-06T09:05:17Z")

</div>

See also

```julia
julia> Meta.@lower [1, 1.0, 1f0]
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Base.vect(1, 1.0, 1.0f0)
└── return %1
))))

help?> Base.vect
  vect(X...)

  Create a Vector with element type computed from the promote_typeof of the argument, containing the argument list.

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 6, 2023, 9:23am UTC](https://discourse.julialang.org/t/why-are-the-inferred-types-different-between-collect-and-comprehension/92590/4 "2023-01-06T09:23:10Z")

</div>

Related to this:

> <https://github.com/JuliaLang/julia/issues/46092>
>
> https://github.com/JuliaLang/julia/blob/82c3a6f50c23ccd541167a490797690bcd5deb3c…/base/array.jl#L128-L132
> 
> Since \`promote\_typeof\` is neither documented, nor assigned a docstring, it's hard to understand what the \`eltype\` of \`vect\` should be, and if this may change in unpredictable ways.
> \`\`\`julia
> julia\> VERSION
> v"1.9.0-DEV.998"
> \`\`\`
