# Correctly dispatching on a variable number of 2-tuples with mixed types

**URL:** https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974
**Category:** General Usage
**Tags:** question
**Created:** [June 21, 2024, 1:32pm UTC](https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974 "2024-06-21T13:32:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![alonsoC1s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alonsoc1s/32/25819_2.png) [@alonsoC1s](https://discourse.julialang.org/u/alonsoC1s)
#### Post date: [June 21, 2024, 1:32pm UTC](https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974/1 "2024-06-21T13:32:28Z")

</div>

I am currently trying to come up with a function signature that would let me take an arbitrary number of 2-tuples where the types of the elements are not necessarily the same. I know that with the signature

```julia
function f(dom::Vararg{Tuple{T, T}, D} where {D, T <: Real}
   ...
end

```

I can dispatch on any number of tuples as long as their elements are all the same type. For instance, (-2.0, 2.0), (-1.0, 1.0) and so on.

I would like to catch elements of the form (-1, 1.0), (1, 1.0) for instance. If only to explicitly throw an argument error, but preferably to promote all the elements and call the type-stable method defined earlier.

I have tried with this signature

```julia
function f(dom::Vararg{<:Tuple{Real, Real}, D} where {D <: Real}
    ...
end

```

and

```julia
function f(dom::Vararg{<:Tuple{<:Real, <:Real}, D} where {D <: Real}
    ...
end

```

But I get this error when trying to call it

```julia
julia> f((1.0, 1), (1.0, 1.0)

ERROR: MethodError: no method matching f(::Tuple{Float64, Int64}, ::Tuple{Float64, Float64})

Closest candidates are:
    f(::Tuple{Real, Real}...) where D <: Real
      @ REPL
    f(::Tuple{T, T}...) where {D, T <: Real}
      @ REPL

```

I have read the [documentation on diagonal types](https://docs.julialang.org/en/v1/devdocs/types/#Diagonal-types), but I couldn’t come up with the correct signature myself.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [June 21, 2024, 2:40pm UTC](https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974/2 "2024-06-21T14:40:08Z")

</div>

Not sure whether this does what you want:

```julia
# 2-element tuples of various types -> promote them to common type
function foo(dom::Vararg{Tuple{Any,Any}})
    common_type = reduce(promote_type, promote_type(typeof(t[1]), typeof(t[2])) for t in dom)
    @info "" common_type
    foo(((convert(common_type, t[1]), convert(common_type, t[2])) for t in dom)...)
end

# all tuples have the same type
function foo(dom::Vararg{Tuple{T, T}}) where T
    @info "final" T
end

```

Now you can do:

```julia-repl
julia> foo((1,1),(1,1))
┌ Info: final
└ T = Int64

julia> foo((1,1),(1,1.0))
┌ Info: 
└ common_type = Float64
┌ Info: final
└ T = Float64

```

---

<div class="post-metadata">

### Author: ![alonsoC1s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alonsoc1s/32/25819_2.png) [@alonsoC1s](https://discourse.julialang.org/u/alonsoC1s)
#### Post date: [June 21, 2024, 4:00pm UTC](https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974/3 "2024-06-21T16:00:18Z")

</div>

I think this fails with mixed non-numeric tuples, like (1.0, “1”), perhaps it works by making it `Tuple{Real, Real}`, since the function is expecting numbers after all

Thanks especially for the `common_type` function, I was uncertain of how to do that

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [June 21, 2024, 7:27pm UTC](https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974/4 "2024-06-21T19:27:30Z")

</div>

What is the desired behavior for a tuple like `(1,"1")`? Use `parse(numeric_type, "1")`?  
Honestly I think at some point you should put the burden on the user of this function to provide sane types instead of trying to handle every possible type you can think of 😅

---

<div class="post-metadata">

### Author: ![alonsoC1s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alonsoc1s/32/25819_2.png) [@alonsoC1s](https://discourse.julialang.org/u/alonsoC1s)
#### Post date: [June 22, 2024, 11:52am UTC](https://discourse.julialang.org/t/correctly-dispatching-on-a-variable-number-of-2-tuples-with-mixed-types/115974/5 "2024-06-22T11:52:40Z")

</div>

You might be right 😅, I was trying to catch all those wrong combinations to fail gracefully, but It makes more sense to document it
