# Correct way to constrain the length of tuples in type definitions?

**URL:** <https://discourse.julialang.org/t/correct-way-to-constrain-the-length-of-tuples-in-type-definitions/18785>\
**Category:** New to Julia\
**Created:** [December 18, 2018, 6:02am UTC](https://discourse.julialang.org/t/correct-way-to-constrain-the-length-of-tuples-in-type-definitions/18785 "2018-12-18T06:02:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [December 18, 2018, 6:02am UTC](https://discourse.julialang.org/t/correct-way-to-constrain-the-length-of-tuples-in-type-definitions/18785/1 "2018-12-18T06:02:46Z")

</div>

Suppose I want to define a type `People` that has two fields: `names` and `ages`. I would like `names` to hold a `Tuple` of `String` objects, and `ages` to hold a `Tuple` of `Int` objects. I would like both tuples to be of the same length.

I am currently parametrizing `People` and using the type parameter as the length of each `Tuple`.

```julia
struct People{N}
    names::NTuple{N, String}
    ages::NTuple{N, Int64}
end

```

Is this approach “correct”? I am asking because type parameters are supposed to define a family of types, and I am not exactly using the parameter for that purpose in this situation. Is there a nicer way of achieving the same result?

Thanks!

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 18, 2018, 6:08am UTC](https://discourse.julialang.org/t/correct-way-to-constrain-the-length-of-tuples-in-type-definitions/18785/2 "2018-12-18T06:08:16Z")

</div>

Looks alright to me. You may also want to use an array instead and check the fields are of the same length at runtime depending on what you will do with these structs.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 18, 2018, 7:55am UTC](https://discourse.julialang.org/t/correct-way-to-constrain-the-length-of-tuples-in-type-definitions/18785/3 "2018-12-18T07:55:37Z")

</div>

> [@ArjunNarayanan](#):
>
> Is there a nicer way of achieving the same result?

I would just allow more general types, and use an inner constructor to validate, giving a nicer error than `MethodError`. Eg

```julia
using ArgCheck

struct People{S <: Tuple{Vararg{<:AbstractString}}, T <: Tuple{Vararg{<:Integer}}}
    names::S
    ages::T
    function People(names::S, ages::T) where {S, T}
        @argcheck length(names) == length(ages)
        new{S, T}(names, ages)
    end
end

```

```julia
julia> People(("A", "B"), (25, 30))
People{Tuple{String,String},Tuple{Int64,Int64}}(("A", "B"), (25, 30))

julia> People(("A", ), (25, 30))
ERROR: ArgumentError: length(names) == length(ages) must hold. Got
length(names) => 1
length(ages) => 2
Stacktrace:
 [1] macro expansion at /home/tamas/.julia/packages/ArgCheck/BUMkA/src/checks.jl:165 [inlined]
 [2] People(::Tuple{String}, ::Tuple{Int64,Int64}) at ./REPL[8]:5
 [3] top-level scope at none:0

```
