The review for SE-0399: Tuple of value pack expansion ran from May 16...29, 2023. Overall feedback was light—mostly consisting of clarifications—and positive. The language steering group has decided to accept the proposal.
Thank you for participating in the review process!
Hi, I'm just checking the specifications of variadic generics and pack expansion, and I noticed that while SE-0393 clearly states that
[repeat each value] is a valid use of pack expansion, SE-0399 states that this is a future direction.
Pack expansion expressions can appear in any position that naturally accepts a list of expressions, including comma-separated lists and top-level expressions. This includes the following:
- Call arguments, e.g.
generic(repeat each value)
- Subscript arguments, e.g.
subscriptable[repeat each index]
- The elements of a tuple value, e.g.
(repeat each value)
- The elements of an array literal, e.g.
[repeat each value]
Pack repetition patterns for arrays
If all elements in a type argument pack are the same or share a conformance, then it should be possible to declare an Array value using a value pack.
I would like to know the current status of such usage. Can it be considered as accepted but not implemented, or does it require additional proposals?
Very good pick-up. I’ll defer to the authors for comment on details about the implementation status.
It’s worth noting that array literals and arrays aren’t the same thing. My read of the text here is that
[repeat each value] is approved as part of the grammar, but that the proposals do not include actually initializing any value from that literal. Thus, any work on the latter would be the subject of later proposals.
It seems to me that in the WWDC compiler, you can:
- Pass a value pack expansion to another (or the same) variadic generic function
- Turn a value pack expansion into a tuple.
- Create types generic over tuple type packs, as demonstrated in the Generalize APIs with parameter packs WWDC session (“error: generic types with parameter packs are experimental”, with no indication of how to experiment with them)
- Use a value pack expansion as the index of a variadic generic subscript (compiler returns non-zero code without a diagnostic or crash)
- Use a value pack expansion in an array literal, even all the types are the same¹ (“error: value pack expansion can only appear inside a function argument list or tuple element”, which seems like a grammar error rather than a constructing-things-from-it error)
- Pass a value pack expansion to a value-variadic function, even if all the types are the same¹ (a natural usage that is not in the list from SE-0393).
So right at this moment, what we have is a fancy tuple.
¹ By “types are the same”, I mean where this is locally knowable, as in
repeat String(describing: each item).
I have succeeded in mapping a value pack into an array as side effect of mapping it into a tuple (of
Voids), but it does not spark joy.
Nice! I could also use similar way to implement some algorithms, like variadic
What is sad is that this escape cannot used for value variadic functions, so I had to use ugly workaround when implementing
(BTW, I found that with
-enable-experimental-feature VariadicGenerics flag variadic generic types can be compiled without “error: generic types with parameter packs are experimental”)