# Method error with Pair including abstract type

**URL:** <https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232>\
**Category:** New to Julia\
**Created:** [June 1, 2021, 9:43pm UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232 "2021-06-01T21:43:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robireton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robireton/32/22684_2.png) [@robireton](https://discourse.julialang.org/u/robireton)\
**Post date:** [June 1, 2021, 9:43pm UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232/1 "2021-06-01T21:43:04Z")

</div>

My expectations seem to be wrong about how defining a method for a Pair that includes an abstract type works. I want to be able to pass a Pair that comprises a Symbol and a Number, but without being specific about Int, Float, etc. I have the following sample code:

```nohighlight
println(":x is a Symbol: ", isa(:x, Symbol))
println("1 is a Number: ", isa(1, Number))
foo(a::Pair{Symbol, Number}) = println(a)

foo(:x => 1)

```

which produces output like:

```shell
✗1 % julia reference/dispatch-example.jl
:x is a Symbol: true
1 is a Number: true
ERROR: LoadError: MethodError: no method matching foo(::Pair{Symbol, Int64})
Closest candidates are:
  foo(::Pair{Symbol, Number}) at dispatch-example.jl:3
Stacktrace:
 [1] top-level scope
   @ dispatch-example.jl:5
in expression starting at dispatch-example.jl:5

```

I don’t understand why my `Pair{Symbol, Int64}` doesn’t match `Pair{Symbol, Number}`

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [June 1, 2021, 9:48pm UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232/2 "2021-06-01T21:48:55Z")

</div>

Here’s one way to make it work:

```julia
julia> foo(a::Pair{Symbol, T} where T<:Number) = println(a)
foo (generic function with 1 method)

julia> foo(:x => 1)
:x => 1

julia>

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 1, 2021, 10:13pm UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232/3 "2021-06-01T22:13:54Z")

</div>

> [@robireton](#):
>
> I don’t understand why my `Pair{Symbol, Int64}` doesn’t match `Pair{Symbol, Number}`

This is a general property of _all_ parametric types in Julia (except `Tuple`, which is its own thing), called _invariance_. You can read all about it here: [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types) In particular, it talks about a similar case:

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 2, 2021, 12:09am UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232/4 "2021-06-02T00:09:46Z")

</div>

Maybe this helps: [Vector{Int} \<: Vector{Real} is false??? · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/typevariance/)

The argument there is about vectors, but the same holds for pairs.

---

<div class="post-metadata">

**Author:** ![robireton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robireton/32/22684_2.png) [@robireton](https://discourse.julialang.org/u/robireton)\
**Post date:** [June 2, 2021, 12:57am UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232/5 "2021-06-02T00:57:13Z")

</div>

I don’t think I realized that I had ventured into parametric type territory! Thanks so much for the solution and the explanations.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 2, 2021, 2:57am UTC](https://discourse.julialang.org/t/method-error-with-pair-including-abstract-type/62232/6 "2021-06-02T02:57:52Z")

</div>

Yup, Julia is relatively unusual in that things like `Pair` are not some special built-in object but just another regular `struct` like any other you might create yourself. You can see the entire definition of `Pair` here:

[https://github.com/JuliaLang/julia/blob/58ffe7e3ed3a93a9d816097548e785284f57fbd4/base/pair.jl#L3-L15](https://github.com/JuliaLang/julia/blob/58ffe7e3ed3a93a9d816097548e785284f57fbd4/base/pair.jl#L3-L15)
