# Variadic Generics

**URL:** <https://forums.swift.org/t/variadic-generics/20320>\
**Category:** Pitches\
**Tags:** generics\
**Created:** [February 10, 2019, 12:42pm UTC](https://forums.swift.org/t/variadic-generics/20320 "2019-02-10T12:42:29Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [March 4, 2019, 11:28pm UTC](https://forums.swift.org/t/variadic-generics/20320/13 "2019-03-04T23:28:57Z")

</div>

> [@technicated](#):
>
> I think that variadic generics can be represented as tuples once "instantiated", and there is a similar precedent in the language too with _variadics function_ argument becoming `Array` s. I say "I think" because I'm not actually sure that this is what the manifesto suggests - given the iterators example in which it talks about "zero or more stored properties" instead of a tuple of iterators instances.
> 
> ```swift
> struct Variadic<...T> {
> var (...items): (T...)
> }
> 
> ```

I sorta regret writing the example in that way. Swift shouldn't need the prefix "...", and that definition of `items` is probably wrong. If we're using "...", that example should likely be written as:

```swift
struct Variadic<T...> {
  var items: (T...)
}

```

Same thing here:

> [@technicated](#):
>
> var (...iterators): (Iterators...)

we don't need the prefix "...", and probably don't even want the notion of it in the language. `iterators` can be described as:

```swift
var iterators: (Iterators...)

```

so we're forming a tuple type from the types in `Iterators` and storing that.

> [@technicated](#):
>
> guard let values = (iterators.next()...) else { // call "next" on each of the iterators, put the results into a tuple named "values" reachedEnd = true return nil }

You are correct; my example here was bogus. I'll take a pass through this part of the generics manifesto so _at least_ it's not as misleading and wrong. Thank you!

> [@technicated](#):
>
> We could use the same ... operator for tuple splatting. Other than the example of the Manifesto, in which a function with variadic generics is called expanding the arguments passed to it, the feature might be useful to allow one to write something like this:
> 
> ```swift
> let (...head, tail) = someTuple
> // `someTuple` is a tuple containing N elements
> // `head` is a tuple containing the first N-1 elements of `someTuple`
> // `tail` is a single value (not a tuple) containing the last element of `someTuple`
> 
> ```

This may not work so well in a generic context unless you had a way to say "I know that `someTuple` has at least one element".

When it comes to tuple splatting, I think what we really want is a way to take a tuple and expand it into N separate arguments, e.g.,

```swift
let someTuple = (1, 3.14159, "Hello")
f(someTuple...). // equivalent to f(1, 3.14159, "Hello")

```

It's plausible that we could integrate that with variadic generics if, say, a "function parameter pack" was represented as a tuple and all tuples could be expanded like that.

Doug

---

_[View the full topic](https://forums.swift.org/t/variadic-generics/20320)._
