# Function signature with tuple argument of a given length

**URL:** <https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631>\
**Category:** General Usage\
**Created:** [October 23, 2017, 5:48pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631 "2017-10-23T17:48:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [October 23, 2017, 5:48pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631/1 "2017-10-23T17:48:36Z")

</div>

Hi,

I want to express that a function accepts as first argument a tuple of length consistent with the type parameter of the second argument. This

```julia
function foo(x::NTuple{M}, b::SomeOtherType{M}) where {M} end

```

is not what I want, because it restricts the type of the entries of `x` to be the same.

Any ideas?

Thanks,

Davide

---

<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:** [October 23, 2017, 6:13pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631/2 "2017-10-23T18:13:34Z")

</div>

You can use a `Tuple{Vararg{Any, N}}`:

```julia
julia> f(x::Tuple{Vararg{Any, N}}, y::Tuple{Vararg{Any, N}}) where {N} = println(N)
f (generic function with 1 method)

julia> f((1,2.0), ("hello", π))
2

```

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [October 23, 2017, 6:22pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631/3 "2017-10-23T18:22:49Z")

</div>

Thanks.

This could be a good edit to the [docs](https://docs.julialang.org/en/stable/manual/types/#Vararg-Tuple-Types-1).

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [October 23, 2017, 7:02pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631/4 "2017-10-23T19:02:05Z")

</div>

I believe what you are looking for is this.

```julia
function foo(x::NTuple{N,M}, b::SomeOtherType{M}) where {N,M} end

```

NTuple{M} is and alias for a NTuple of length N, but not type.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [October 23, 2017, 7:44pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631/5 "2017-10-23T19:44:20Z")

</div>

Just use `f(x::NTuple{M,Any}, b::SomeOtherType{M}) where {M}`, now the entries of `x` can be of `Any` type.

(which is equivalent to the solution of @rdeits since `NTuple{M,Any} = Tuple{Vararg{Any,M}}`).

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [October 23, 2017, 7:53pm UTC](https://discourse.julialang.org/t/function-signature-with-tuple-argument-of-a-given-length/6631/6 "2017-10-23T19:53:47Z")

</div>

This will only match tuples where the elements have the same type.
