Tuple struct with N attributes

Say I need to create a tuple struct (so, with unnamed fields) with N parameters of type Foo, is there a syntax that would allow me to write such a thing ?

I mean, something like this:

MyStruct(Foo, Foo, Foo, Foo, ... N times )
/// Looking for a syntax that would allow me to write it like this
MyStruct(Foo * N) // Invalid syntax, obviously

I’m open to solutions involving macros, as I think that’s my only escape… ? :slight_smile:

Why not use an array?

struct MyStruct([Foo; N]);

You could also use type aliases to make it easier to write out.

type Foo = ...;
struct MyStruct(Foo, Foo, ...);

Macros won’t help you here, because it isn’t possible to branch on a value inside a macro.


A (long) macro (if you have an upper bound on N) or a proc_macro could actually work, but your resulting tuple would still be kind of unusable: you wouldn’t be able to access the Nth element unless you also heavily relied on macros. That’s why for a (compile-time variable) const parameter, the array is a far better alternative than a homogeneous N-tuple (there should be no bound check when indexing with a const expression, for instance).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.