Struct with different-sized tuple field

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

struct mystruct
  values::NTuple(n, Int)

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.

You probably want

struct mystruct{N}
  values::NTuple{N, Int}

do you not want {} instead of ()?

1 Like

Good catch.

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.)

1 Like