# How to deal with parameters of the type Vector{Tuple}?

**URL:** <https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968>\
**Category:** New to Julia\
**Created:** [January 26, 2021, 11:31am UTC](https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968 "2021-01-26T11:31:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [January 26, 2021, 11:31am UTC](https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968/1 "2021-01-26T11:31:09Z")

</div>

Hi,

I’m writing some code where I expect a vector of 2-element tuples but I can’t figure out how to type the parameters in such a way that it’s clear what needs to be passed but not so restrictive that type promotion is thrown out of the window.

I thought the following would work (I simplified the code to the minimum):

``  
f(v::Vector{Tuple{Real, Real}}) = v

f([(1, 1)])

```julia

But this results in the following error:

```

ERROR: MethodError: no method matching f(::Array{Tuple{Int64,Int64},1})  
Closest candidates are:  
f(::Array{Tuple{Real,Real},1}) at none:1  
Stacktrace:  
[1] top-level scope at none:1

```julia

So I thought I'd simplify things as follows:

```

f(v::Vector{Tuple}) = v

```julia

But I get the same type of error:

```

ERROR: MethodError: no method matching f(::Array{Tuple{Int64,Int64},1})  
Closest candidates are:  
f(::Array{Tuple,1}) at none:1  
f(::Array{Tuple{Real,Real},1}) at none:1  
Stacktrace:  
[1] top-level scope at none:1

```julia

Is there any way that I can type this in such a way that I can actually call the function with any type of Real elements in the tuples?

Thanks in advance,
Stef
```

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [January 26, 2021, 11:39am UTC](https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968/2 "2021-01-26T11:39:31Z")

</div>

There you go!

`function f(v::Array{Tuple{T1,T2},1}) where {T1,T2 <: Real}`

I had the same problem yesterday. It turns out that using `Real` as is does not work because of a concept called ‘invariance’ (see the documentation on [Types](https://docs.julialang.org/en/v1/manual/types/)).

So, what you have to do is indicate the compiler that it can be **any type** that belongs to `Real`. That is what the `where {T1,T2 <: Real}` part means.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [January 26, 2021, 12:16pm UTC](https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968/4 "2021-01-26T12:16:11Z")

</div>

Thanks! I’m going to be using this quite a lot I think 🙂

---

<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:** [January 26, 2021, 12:23pm UTC](https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968/5 "2021-01-26T12:23:09Z")

</div>

I’d go for `f(v::Vector{T}) where T<:Tuple{Real,Real}`. That works because tuples are covariant on parameters (i.e. `T<:Pair{Real,Real}` wouldn’t work the same, and one would need `T<:Pair{<:Real, <:Real})`.

If both tuple elements are expected to have the same type, `f(v::Vector{T}) where {T<:Tuple{R,R}, R<:Real}` may be used.

`Vector{Tuple}` is a concrete type that can hold any tuple, so, from Julia type system point of view, it is different from `Vector{Tuple{Int,Int}}` which can only hold tuples of two `Int`s.

Also, I’d recommend using `AbstractVector` in the signature in case you’d like to pass a vector view at some point in the future.

---

<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 26, 2021, 3:32pm UTC](https://discourse.julialang.org/t/how-to-deal-with-parameters-of-the-type-vector-tuple/53968/6 "2021-01-26T15:32:12Z")

</div>

Note that

```julia
f(v::Vector{Tuple{T1,T2}}) where {T1,T2 <: Real} = v

```

does not restrict the parameter `T1`, so for example

```julia
julia> f([("abc",2)])
1-element Array{Tuple{String,Int64},1}:
 ("abc", 2)

```

As suggested above, perhaps what you want is

```julia
julia> f(v::Vector{<:Tuple{Real,Real}}) = v
f (generic function with 2 methods)

julia> f([(2,1)])
1-element Array{Tuple{Int64,Int64},1}:
 (2, 1)

julia> f([(2.0,1)])
1-element Array{Tuple{Float64,Int64},1}:
 (2.0, 1)

```
