Skip to content

Commit c75010a

Browse files
committed
Add a section about multi-dimensional packs.
1 parent 8b472d8 commit c75010a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

vision-documents/variadic-generics.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,26 @@ func iterate(over tuple: Tuple<Int, String, Bool>) {
281281
}
282282
```
283283

284+
### Multi-dimensional packs
285+
286+
Enabling associated type packs is necessary for writing protocols that express an interface for variadic generic types. For example:
287+
288+
```swift
289+
protocol HeterogeneousSequence<Element...> {
290+
associatedtype Element...
291+
}
292+
293+
struct List<Element...>: HeterogeneousSequence {}
294+
```
295+
296+
In full generality, associated type packs introduce multi-dimensional packs into the language:
297+
298+
```swift
299+
func twoDimensional<T...>(_ t: T...) where T: HeterogeneousSequence {}
300+
```
301+
302+
In the above generic signature for `twoDimensional`, `T.Element` is a multi-dimensional pack. It may be useful to introduce a way to express pack expansions of multi-dimensional packs, e.g. to flatten all of the `Element` values into a single list, but it's unclear how this might be expressed. To enable associated type packs in the short term, it may be possible to restrict conformance requirements involving protocols with associated type packs to scalar, non-pack type parameters.
303+
284304
### Accessing tuple elements as a pack
285305

286306
To achieve the goal of using variadic generics to generalize tuple types, this design includes the ability to access the elements of a tuple value as a value pack, unlocking all the same expressivity for tuples without introducing an additional set of operations for mapping, iteration, concatenation, and de-structuring.

0 commit comments

Comments
 (0)