Is there a way to reverse a tuple?

Is there a way (in Swift 6) to reverse the members of a tuple, i.e. given a tuple of type (A, B, C), return a tuple of (C, B, A)? It still needs to work when the variadic length is 0 or 1.

While there seems to be a way of converting a tuple to array and then back using Mirror, I'm curious why are you using a tuple where it should have been an array in the first place?

Tuples are closer to structs more than arrays since they are not homogeneous, so inverting a tuple is pretty much like wanting to invert a struct.

I thought it would be possible to do this with parameter packs, but so far I haven't been able to find the correct incarnation :joy: On the other hand I found a compiler crash.
I would be curious to see a solution to this using generic parameter packs :slight_smile:

1 Like

I need to iterate a parameter pack backwards. All the members are Sequence objects with the same Element type, but not necessarily a shared top-level type.

Hmm, could I store every operand as an AnySequence<MyElement> instead? Would “any Sequence<MyElement>” work? If both work, which option would be better?