# How to dispatch on a Tuple with unknown length, but elements have same abstract type?

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017>\
**Category:** General Usage\
**Tags:** type, tuple, parametric-types, diagonal-rule\
**Created:** [January 16, 2023, 9:12am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017 "2023-01-16T09:12:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [January 16, 2023, 9:12am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017/1 "2023-01-16T09:12:26Z")

</div>

Consider this example:

```julia
a = (complex(1,2), complex(1.0, 2.0))

```

The length of `a` is not fixed to 2, but could be arbitrary.  
Is there a way to dispatch on this?  
I tried

```julia
a isa NTuple{N, <: Complex} where N

```

but this is obviously false, because `a` isn’t a NTuple.  
It works with

```julia
a = (complex(1.0,2.0), complex(1.0, 2.0))

```

though.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 16, 2023, 9:18am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017/2 "2023-01-16T09:18:21Z")

</div>

```julia
julia> a = (complex(1,2), complex(3.0,4.0))
(1 + 2im, 3.0 + 4.0im)

julia> a |> typeof
Tuple{Complex{Int64}, ComplexF64}

julia> a isa NTuple{N, <: Complex} where N
false

```

because

```julia
julia> complex(1,2) |> typeof
Complex{Int64}

julia> complex(3.0,4.0) |> typeof
ComplexF64 (alias for Complex{Float64})

```

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 16, 2023, 9:22am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017/3 "2023-01-16T09:22:34Z")

</div>

Sorry, overlooked the `abstract type` in the question.

Might wanna use

```julia
julia> a isa NTuple{N,Complex{<:Real}} where N
true

```

---

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [January 16, 2023, 11:26am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017/4 "2023-01-16T11:26:42Z")

</div>

I figured out that I have to leave out the first `<:` in the second argument of the NTuple.  
This works, too:

```julia
a isa NTuple{N, Complex} where N

```

---

<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 16, 2023, 11:42am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-a-tuple-with-unknown-length-but-elements-have-same-abstract-type/93017/5 "2023-01-16T11:42:33Z")

</div>

The rules for `Tuple` subtyping are special: tuples are covariant wrt their type parameters.  
That means, `a` is an `NTuple{2, Complex}`. On the other hand, `a isa NTuple{2,T} where {T<:Complex}` is false because of the [diagonal rule](https://docs.julialang.org/en/v1/devdocs/types/#Diagonal-types).
