# NTuple length dependent on parameter of type

**URL:** <https://discourse.julialang.org/t/ntuple-length-dependent-on-parameter-of-type/7338>\
**Category:** General Usage\
**Tags:** tuple, parametric-types, ntuple\
**Created:** [November 27, 2017, 3:27pm UTC](https://discourse.julialang.org/t/ntuple-length-dependent-on-parameter-of-type/7338 "2017-11-27T15:27:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [November 27, 2017, 3:27pm UTC](https://discourse.julialang.org/t/ntuple-length-dependent-on-parameter-of-type/7338/1 "2017-11-27T15:27:38Z")

</div>

Is it possible to have a type say:

```julia
struct A{B}
field::NTuple{N, Int}
end

```

Say B is always a number so A{5}, A{7} and so on.  
Is it possible to specify in the type definition that N depends on B, in some manner e.g:

```julia
struct A{B}
field::NTuple{(B / 64), UInt64}
end

```

The above doesn’t work, but hopefully shows what I’d like to enforce.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 27, 2017, 3:44pm UTC](https://discourse.julialang.org/t/ntuple-length-dependent-on-parameter-of-type/7338/2 "2017-11-27T15:44:35Z")

</div>

No, that is not possible. See [Algebraic constraints for type parameters](https://discourse.julialang.org/t/algebraic-constraints-for-type-parameters/6409).

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [November 27, 2017, 4:16pm UTC](https://discourse.julialang.org/t/ntuple-length-dependent-on-parameter-of-type/7338/3 "2017-11-27T16:16:57Z")

</div>

As @mauro3 pointed out, that is not possible.  
But you are able to enforce that by defining a inner constructor:

```julia
julia> struct A{B,N}
       field::NTuple{N,Int}
       function A{B}(x) where {B}
         N = B÷64
         N == length(x) || throw(ArgumentError("Tuple of incorrect length"))
         return new{B,N}(x)
       end
       end

julia> x = (1,2)
(1, 2)

julia> y = A{128}(x)
A{128,2}((1, 2))

julia> z = A{64}(x)
ERROR: ArgumentError: Tuple of incorrect length
Stacktrace:
 [1] A{64,N} where N(::Tuple{Int64,Int64}) at ./REPL[2]:5

julia> z = A{64}((1,))
A{64,1}((1,))

```
