# Is it possible to constrain variadic generics to a particular type

**URL:** <https://forums.swift.org/t/is-it-possible-to-constrain-variadic-generics-to-a-particular-type/76333>\
**Category:** Using Swift\
**Tags:** variadic-generics\
**Created:** [December 2, 2024, 3:12pm UTC](https://forums.swift.org/t/is-it-possible-to-constrain-variadic-generics-to-a-particular-type/76333 "2024-12-02T15:12:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjhomer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bjhomer/32/313_2.png) [@bjhomer](https://forums.swift.org/u/bjhomer)\
**Post date:** [December 2, 2024, 3:12pm UTC](https://forums.swift.org/t/is-it-possible-to-constrain-variadic-generics-to-a-particular-type/76333/1 "2024-12-02T15:12:04Z")

</div>

I have a function that converts an array into a tuple, if the element count is correct. For example:

```swift
let array = [1, 2, 3]
guard let (a, b, c) = array.explode() else {
  fatalError("The array must not have had 3 elements")
}
a == 1
b == 2
c == 3

```

Currently, I have this implemented by a series of functions for the different tuple sizes:

```swift
extension Collection {
  func explode() -> (Element, Element)?
  func explode() -> (Element, Element, Element)?
  func explode() -> (Element, Element, Element, Element)?
  // etc...
}

```

I was looking into implementing this using variadic generics instead, but either I haven't found the right incantation, or it's not supported yet. Here's what I'm trying:

```swift
// Error: Same-element constraints are not yet implemented
func explode<each T>() -> (repeat each T)
  where repeat each T == Element
{
  var itr = self.makeIterator()
  return (repeat itr.next() as! each T)
}

```

I can remove the `where` clause and write out all the types manually like this:

```swift
let (a, b, c): (Int, Int, Int) = [1, 2, 3].explode()!

```

But that becomes quite unwieldy as the number of elements scales. I already have manual variants implemented for 2–6 elements, so I'm really hoping to get something that can handle large numbers better.

Is there a way to write this right now?

---

<div class="post-metadata">

**Author:** ![bjhomer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bjhomer/32/313_2.png) [@bjhomer](https://forums.swift.org/u/bjhomer)\
**Post date:** [December 2, 2024, 3:18pm UTC](https://forums.swift.org/t/is-it-possible-to-constrain-variadic-generics-to-a-particular-type/76333/2 "2024-12-02T15:18:46Z")

</div>

I realize that my current implementation attempt also doesn't have a way to return `nil` if the element count doesn't match.

---

<div class="post-metadata">

**Author:** ![RandomHashTags](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/randomhashtags/32/24259_2.png) [@RandomHashTags](https://forums.swift.org/u/RandomHashTags)\
**Post date:** [December 2, 2024, 3:40pm UTC](https://forums.swift.org/t/is-it-possible-to-constrain-variadic-generics-to-a-particular-type/76333/3 "2024-12-02T15:40:00Z")

</div>

I don't think so at the moment.

> [@bjhomer](#):
>
> I realize that my current implementation attempt also doesn't have a way to return `nil` if the element count doesn't match.

Something like this:

```swift
extension Collection {
    func explode<each T>(into explodeCount: Int) throws -> (repeat each T) {
        guard count == explodeCount else { throw CustomError() }
        var itr = self.makeIterator()
        return (repeat itr.next() as! each T)
    }
}

```

Usage (type annotation is required):

```swift
let array:[Int] = [1, 2, 3]
guard let (a, b, c):(Int, Int, Int) = try? array.explode(into: 3) else { return }

```

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 2, 2024, 4:08pm UTC](https://forums.swift.org/t/is-it-possible-to-constrain-variadic-generics-to-a-particular-type/76333/4 "2024-12-02T16:08:11Z")

</div>

> [@bjhomer](#):
>
> I was looking into implementing this using variadic generics instead, but either I haven't found the right incantation, or it's not supported yet.

It's not supported yet.

> [@bjhomer](#):
>
> Is there a way to write this right now?

You can _almost_ fake it, with a generic type alias, like this:

```swift
typealias SameElt<T, U> = U

func sameElt<T, U>(_ t: T, _ u: U) -> U { return u }

func explode<each T, U>(_ array: [U]) -> (repeat SameElt<each T, U>) {
  var itr = array.makeIterator()
  return (repeat sameElt((each T).self, itr.next()!))
}

```

However, this produces a diagnostic that the generic parameter `each T` does not appear in the signature of `explode()`, and indeed this reveals the underlying limitation.

The diagnostic is correct because while the caller can fix the _length_ of `each T` by specifying a tuple type as the contextual result of the call expression, the _element types_ of `each T` cannot be fixed from the function's signature in any way, and while the implementation of `explode()` happens to not actually depend on what element types are, it _could_, and the caller has no way of knowing this.

You can change the declaration of `explode()` so that it passes in a dummy pack for `each T`, which is then just ignored:

```swift
func explode<each T, U>(_: (repeat (each T)).Type, _ array: [U]) -> (repeat SameElt<each T, U>) {

```

With the above, this in fact works:

```swift
let (a, b, c) = explode(((), (), ()).self, [1, 2, 3])

print("a = \(a), b = \(b), c = \(c)")

```

However, it's awkward to use because of the `((), (), ())` (and in fact, any tuple metatype of length 3 could be used, `(Int, Float, Bool).self` for example, again because the element types are ignored.)

There is a [preliminary implementation](https://github.com/swiftlang/swift/pull/70227) of same-element requirements in the Requirement Machine, but what's missing is support in various other places. For example, when IRGen lowers the function signature, it needs to consider the case where we must pass in the length of a pack, but not its element metadata, as would be the case with `explode()` above.
