# Incorrect concrete type for Vector{Pair{Any, Any}}?

**URL:** https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885
**Category:** General Usage
**Tags:** type, potential-bug
**Created:** [December 10, 2021, 10:37am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885 "2021-12-10T10:37:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![anicusan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anicusan/32/30094_2.png) [@anicusan](https://discourse.julialang.org/u/anicusan)
#### Post date: [December 10, 2021, 10:37am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/1 "2021-12-10T10:37:40Z")

</div>

Hello,

My question’s rather simple:

```julia
julia> [1=>2]
1-element Vector{Pair{Int64, Int64}}:
 1 => 2

julia> [1=>2] isa Vector{Pair{Any, Any}}
false

```

Why is `Vector{Pair{Int64, Int64}}` not a subtype of `Vector{Pair{Any, Any}}`?

And if useful, here is my Julia version:

```julia
julia> versioninfo()
Julia Version 1.6.4
Commit 35f0c911f4 (2021-11-19 03:54 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_DEPOT_PATH = /home/andreinicusan/Prog/Julia/builds/julia-1.6.4/packages:
  JULIA_NUM_THREADS = 16

```

Thanks,  
Leonard

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [December 10, 2021, 10:43am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/2 "2021-12-10T10:43:14Z")

</div>

I don’t know the _why_ but

julia\> [1=\>2] isa Vector{Pair{T,T}} where T \<: Any  
true

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [December 10, 2021, 10:48am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/3 "2021-12-10T10:48:05Z")

</div>

Yes, this is correct and expected behaviour. `Vector{Pair{Any, Any}}` is a concrete type that can be instantiated, it would be a vector of pairs of different types. Concrete types cannot have subtypes, therefore `Vector{Pair{Int, Int}}` cannot be its subtype.

This has to do with invariant and contravariant types, you can read more about this here: [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types)

In particular, you can find this warning in the docs:

> This last point is _very_ important: even though `Float64 <: Real` we **DO NOT** have `Point{Float64} <: Point{Real}` .

An alternative convenient test could be

```julia
1.7.0> [1=>2] isa Vector{<:Pair}
true

```

---

<div class="post-metadata">

### Author: ![anicusan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anicusan/32/30094_2.png) [@anicusan](https://discourse.julialang.org/u/anicusan)
#### Post date: [December 10, 2021, 11:04am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/4 "2021-12-10T11:04:58Z")

</div>

Thank you for the very quick replies. It seems odd that `Tuple` is covariant, while `Pair` is invariant; I understand that covariance is special-cased, but why isn’t a type as fundamental as `Pair` - not unlike `Tuple` - made covariant?

I am currently working with a library that asserts types with `constraints::Vector{Pair{Any,Any}}`, so I need to enforce the `Any` quite awkwardly:

```julia
julia> [1=>2, ""=>""][1:end-1]
1-element Vector{Pair{Any, Any}}:
 1 => 2

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [December 10, 2021, 11:19am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/5 "2021-12-10T11:19:02Z")

</div>

A type like `Vector` cannot really be covariant: one cannot pass e.g. `Vector{Int}` everywhere where `Vector{Any}` is expected. This function would fail if you could pass `Vector{Int}` to it:

```julia
f(x::Vector{Any}) = push!(x, "abc")

```

but it works for `Vector{Any}`.

If you do need to create a `Vector{Pair{Any, Any}}` containing pairs of a single type (do you **really** need it?), it’s possible much less awkwardly: `Pair{Any, Any}[1 => 2]`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [December 10, 2021, 11:58am UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/6 "2021-12-10T11:58:40Z")

</div>

> [@anicusan](#):
>
> `Tuple` is covariant

Apparently, `Tuple` is abstract unless its parameters are concrete, so you cannot instantiate it. I am not sure if that makes it contravariant or not, but at least there is nothing preventing it to have subtypes.

> [@aplavin](#):
>
> `Pair{Any, Any}[1 => 2]`

Yes, or, alternatively:

```julia-auto
[Pair{Any, Any}(1,2)]

```

This is a common idiom in Julia, btw, for any type, `T`, you can create a `Vector{T}` by writing `T[...]`

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [December 10, 2021, 12:03pm UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/7 "2021-12-10T12:03:53Z")

</div>

> [@anicusan](#):
>
> I am currently working with a library that asserts types with `constraints::Vector{Pair{Any,Any}}`

That is terrible, BTW. Maybe you can open an issue/PR to have it changed. This is really not idiomatic Julia.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [December 10, 2021, 1:24pm UTC](https://discourse.julialang.org/t/incorrect-concrete-type-for-vector-pair-any-any/72885/8 "2021-12-10T13:24:03Z")

</div>

it might not be bad. sometimes this is what you need to prevent ridiculous amounts of compiler specialization. that said, 90% of the time, this is bad.
