# Comprehension doesn't infer type of parametric type collection when empty

**URL:** <https://discourse.julialang.org/t/comprehension-doesnt-infer-type-of-parametric-type-collection-when-empty/77040>\
**Category:** General Usage\
**Tags:** type, parametric-types\
**Created:** [February 24, 2022, 9:23pm UTC](https://discourse.julialang.org/t/comprehension-doesnt-infer-type-of-parametric-type-collection-when-empty/77040 "2022-02-24T21:23:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [February 24, 2022, 9:23pm UTC](https://discourse.julialang.org/t/comprehension-doesnt-infer-type-of-parametric-type-collection-when-empty/77040/1 "2022-02-24T21:23:53Z")

</div>

When comprehension returns a empty collection of a parametric type, the type is lost.  
In simple code:

```julia
struct A{T}
    a::T
end

vs = [A(1), A(2), A(3), A(1.0), A(2.0), A(3.0)]
#6-element Vector{A}:
# A{Int64}(1)
# A{Int64}(2)
# A{Int64}(3)
# A{Float64}(1.0)
# A{Float64}(2.0)
# A{Float64}(3.0)

# doesn't infer the type
[v for v in vs if v.a == 7]
#Any[]

# infers the type successfully
Iterators.filter(x -> x.a ==7, vs) |> collect
#A[]

```

Is this normal ?

This can ofc break some code since multiple dispatch will fail in code following.

A lot of people seem to point to this issue [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276) when problematic comprehension inference is discussed. Is it also for this situation the case or is it something else ?

---

<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:** [February 25, 2022, 1:57am UTC](https://discourse.julialang.org/t/comprehension-doesnt-infer-type-of-parametric-type-collection-when-empty/77040/2 "2022-02-25T01:57:43Z")

</div>

```julia
eltype(vs)[v for v in vs if v.a == 7]

```

It’d of course be better if `vs` itself were concretely typed, or were a small union of types, e.g.

```julia
vs = Union{A{Int},A{Float64}}[A(1), A(2), A(3), A(1.0), A(2.0), A(3.0)]

```

This isn’t a comprehension capturing closures in the anonymous function. It isn’t capturing anything.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 25, 2022, 5:06am UTC](https://discourse.julialang.org/t/comprehension-doesnt-infer-type-of-parametric-type-collection-when-empty/77040/3 "2022-02-25T05:06:03Z")

</div>

I pasted the code example in replit and labstack for a quick check of behavior on older versions (1.4.1, 1.6.2), and the comprehension is an empty `Vector{A}`. I’m assuming sometime since then, there was a change in comprehension type inference.
