# Struct with different-sized tuple field

**URL:** <https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 20, 2023, 8:42pm UTC](https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282 "2023-01-20T20:42:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronan\_Gautier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronan_gautier/32/46328_2.png) [@Ronan\_Gautier](https://discourse.julialang.org/u/Ronan_Gautier)\
**Post date:** [January 20, 2023, 8:42pm UTC](https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282/1 "2023-01-20T20:42:31Z")

</div>

What is the proper way to define a structure that would look like

```julia
struct mystruct
  n::Int
  values::NTuple(n, Int)
end

```

This does not work and I get the error `ERROR: UndefVarError: n not defined`.  
In my case, it is required that values is an NTuple and not a Vector for performance. But I want to be able to define the structure for various values of n, so n cannot be a constant.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 20, 2023, 8:46pm UTC](https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282/2 "2023-01-20T20:46:04Z")

</div>

You probably want

```julia
struct mystruct{N}
  values::NTuple{N, Int}
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 20, 2023, 9:16pm UTC](https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282/3 "2023-01-20T21:16:55Z")

</div>

> [@Oscar\_Smith](#):
>
> ```julia
> struct mystruct{N}
> values::NTuple(N, Int)
> end
> 
> ```

do you not want `{}` instead of `()`?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 20, 2023, 9:18pm UTC](https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282/4 "2023-01-20T21:18:35Z")

</div>

Good catch.

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [January 21, 2023, 8:04am UTC](https://discourse.julialang.org/t/struct-with-different-sized-tuple-field/93282/5 "2023-01-21T08:04:49Z")

</div>

Be mindful of the downsides of this approach, namely that every differently sized version of this type will be its own fully unique type.

This means it will trigger recompilation every time a new sized version is passed into a function.

If you only have a handful of different sizes of types then this is probably fine. But if they’re varying a lot, the performance penalty of compilation might outweigh the benefit of the having an isbits type. (As always, benchmark rather than assume when considering these kinds of trade offs.)
