# Declaring Pair with one fixed type

**URL:** <https://discourse.julialang.org/t/declaring-pair-with-one-fixed-type/45756>\
**Category:** General Usage\
**Created:** [August 29, 2020, 5:23pm UTC](https://discourse.julialang.org/t/declaring-pair-with-one-fixed-type/45756 "2020-08-29T17:23:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josiah\_Slack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josiah_slack/32/8319_2.png) [@Josiah\_Slack](https://discourse.julialang.org/u/Josiah_Slack)\
**Post date:** [August 29, 2020, 5:23pm UTC](https://discourse.julialang.org/t/declaring-pair-with-one-fixed-type/45756/1 "2020-08-29T17:23:19Z")

</div>

Let’s say that I’d like to declare a function that takes a Pair as its argument, and that I’d like to restrict the kind of Pair that will be matched. For example:

> function foo(p::Pair{String,Any})
> 
> # do some stuff
> 
> end

If I then create a Pair object:

> p = “firstkey”=\>true

And then call foo(p), I get:  
ERROR: MethodError: no method matching foo(::Pair{String,Bool})

Relatedly, I find this confusing:

> Tuple{String, Bool} \<: Tuple{String, Any}  
> returns true

but

> Pair{String, Bool} \<: Pair{String, Any}  
> returns false.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 29, 2020, 6:06pm UTC](https://discourse.julialang.org/t/declaring-pair-with-one-fixed-type/45756/2 "2020-08-29T18:06:27Z")

</div>

`Pair{String}` or `Pair{String, <:Any}` or `Pair{String, T} where T`.  
Maybe `Pair{<:AbstractString, <:Any}` is more appropriate, as some string manipulation functions return `SubString` and you may occasionally use it as the first element of a pair.

Also, read on [type parameter invariance](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types) and [tuple parameter covariance](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types).

---

<div class="post-metadata">

**Author:** ![Josiah\_Slack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josiah_slack/32/8319_2.png) [@Josiah\_Slack](https://discourse.julialang.org/u/Josiah_Slack)\
**Post date:** [August 29, 2020, 6:50pm UTC](https://discourse.julialang.org/t/declaring-pair-with-one-fixed-type/45756/3 "2020-08-29T18:50:16Z")

</div>

Thanks, that’s very helpful, and the suggestion about AbstractString is a good one.
