# Variadic types: "Same-element requirements are not yet supported"

**URL:** <https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285>\
**Category:** Discussion\
**Tags:** variadic-generics\
**Created:** [September 14, 2023, 9:44pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285 "2023-09-14T21:44:09Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![toastersocks](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toastersocks/32/27695_2.png) [@toastersocks](https://forums.swift.org/u/toastersocks)\
**Post date:** [September 14, 2023, 9:44pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/1 "2023-09-14T21:44:09Z")

</div>

I was really excited to replace a lot of my repetitive declarations like:

```swift
func combineLatest<B: Publisher, C: Publisher, D: Publisher, E: Publisher>(_ b: B, _ c: C, _ d: D, _ e: E) -> AnyPublisher<(Output, B.Output, C.Output, D.Output, E.Output), Failure>
    where Self.Failure == B.Failure, B.Failure == C.Failure, C.Failure == D.Failure, D.Failure == E.Failure { ... }

func combineLatest<B: Publisher, C: Publisher, D: Publisher, E: Publisher, F: Publisher>(_ b: B, _ c: C, _ d: D, _ e: E, _ f: F) -> AnyPublisher<(Output, B.Output, C.Output, D.Output, E.Output, F.Output), Failure>
    where Self.Failure == B.Failure, B.Failure == C.Failure, C.Failure == D.Failure, D.Failure == E.Failure, E.Failure == F.Failure { ... }
// repeat, adding more parameters up to nth arity...

```

With one declaration of something like:

```swift
func combineLatest<each PublisherType: Publisher>(_ publishers: repeat each PublisherType) -> AnyPublisher<(Output, repeat (each PublisherType).Output), Failure> where repeat Failure == (each PublisherType).Failure { ... }

```

But I get the error: `Same-element requirements are not yet supported`  
That '_yet_' makes it seem like this is a limitation that might be lifted someday.  
Is this part of a proposal that didn't get implemented completely, or is it an intentional limitation?  
To ask another way, are there any concrete plans to lift this someday? Or would this require an evolution proposal to implement?

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [September 14, 2023, 9:53pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/2 "2023-09-14T21:53:19Z")

</div>

> [@toastersocks](#):
>
> That '_yet_' makes it seem like this is a limitation that might be lifted someday.

> <https://github.com/apple/swift/pull/67465>
>
> SE-0393 supports \_same-element requirements\_. From the proposal:
> 
> \> A same-typ…e requirement where one side is a type parameter pack and the other type is a scalar type that does not capture any type parameter packs is interpreted as constraining each element of the replacement type pack to the same scalar type:
> \> \`\`\`swift
> \> func variadic\<each S: Sequence, T\>(\_: repeat each S) where repeat (each S).Element == T {}
> \> \`\`\`
> \> This is called a \_same-element requirement.\_
> 
> We represent pack elements in the requirement machine through a new singleton \`PackElement\` symbol, written \`\[element\]\`. Same-element requirements are mapped to re-write rules that prefix type parameter pack terms with the \`\[element\]\` symbol.
> 
> For example, given a protocol \`P\` with an associated type \`A\`, the generic signature \`\<each T, U\> where repeat each T: P, repeat (each T).A == U\` maps to the following re-write system:
> 
> \`\`\`
> Rewrite system: {
> \- \[P\].\[P\] =\> \[P\]
> \- \[P\].A =\> \[P:A\]
> \- \[P\].\[P:A\] =\> \[P:A\]
> \- \[P:A\].\[shape\] =\> \[shape\]
> \- τ\_0\_1.\[shape\] =\> \[shape\]
> \- each τ\_0\_0.\[P\] =\> each τ\_0\_0
> \- \[element\].each τ\_0\_0.A =\> τ\_0\_1
> \- each τ\_0\_0.A =\> each τ\_0\_0.\[P:A\]
> \- \[element\].each τ\_0\_0.\[P:A\] =\> τ\_0\_1
> \- \[element\].each τ\_0\_0.\[shape\] =\> \[shape\]
> }
> \`\`\`
> 
> The implementation is gated behind \`-enable-experimental-feature SameElementRequirements\` because the minimal conformance computation doesn't yet support same-element requirements. The current rewrite rules also do not support deriving pack element conformance from a scalar type parameter conformance through same-element requirements. For example, the generic signature \`\<each T, U\> where U: P, repeat each T == U\` maps to the following rewrite system:
> 
> \`\`\`
> Rewrite system: {
> \- \[P\].\[P\] =\> \[P\]
> \- \[P\].A =\> \[P:A\]
> \- \[P\].\[P:A\] =\> \[P:A\]
> \- \[P:A\].\[shape\] =\> \[shape\]
> \- τ\_0\_1.\[shape\] =\> \[shape\]
> \- τ\_0\_1.\[P\] =\> τ\_0\_1
> \- \[element\].each τ\_0\_0 =\> τ\_0\_1
> \- τ\_0\_1.A =\> τ\_0\_1.\[P:A\]
> }
> \`\`\`
> 
> The conformance rule \`τ\_0\_1.\[P\] =\> τ\_0\_1\` and the same-element rule \`\[element\].each τ\_0\_0 =\> τ\_0\_1 \` do not provide a rewrite path to the pack conformance rule \`each τ\_0\_0.\[P\] =\> each τ\_0\_0\`. One possible solution is to use element symbols for all property rules, which would also allow the minimal conformance support to fallout naturally. However, this approach poses a problem for same-type-pack requirements. If we use \`\[element\]\` symbols for same-type-pack requirements, e.g. \`\[element\].each τ\_0\_0 =\> \[element\].each τ\_0\_1\`, then this rule no longer implies a same-shape rule \`each τ\_0\_0.\[shape\] =\> each τ\_0\_1.\[shape\]\`. Stripping the \`\[element\]\` symbols from same-type-pack rewrite rules creates a different set of problems for conformances, because the pack conformance rules contain \`\[element\]\` symbols, which would prohibit applying associated type rewrite rules to non-element pack terms. I'm still exploring strategies for resolving these issues, hence the experimental flag!

I think I know how to fix the outstanding issues mentioned in the PR description, I just need to find a little time to get it over the finish line 🙂

> [@toastersocks](#):
>
> Is this part of a proposal that didn't get implemented completely, or is it an intentional limitation?

The design of same-element requirements is specified in [SE-0393](https://github.com/apple/swift-evolution/blob/main/proposals/0393-parameter-packs.md#generic-requirements).

---

<div class="post-metadata">

**Author:** ![toastersocks](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toastersocks/32/27695_2.png) [@toastersocks](https://forums.swift.org/u/toastersocks)\
**Post date:** [September 14, 2023, 11:07pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/3 "2023-09-14T23:07:02Z")

</div>

> [@hborla](#):
>
> I think I know how to fix the outstanding issues mentioned in the PR description, I just need to find a little time to get it over the finish line 🙂

That's great news! Thanks for your work on this.

> [@hborla](#):
>
> The design of same-element requirements is specified in [SE-0393](https://github.com/apple/swift-evolution/blob/main/proposals/0393-parameter-packs.md#generic-requirements)

Wow, interesting. I was having a hard time figuring out which variadic proposal was the relevant one. Thanks for the link 🙂

---

<div class="post-metadata">

**Author:** ![dehesa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dehesa/32/10911_2.png) [@dehesa](https://forums.swift.org/u/dehesa)\
**Post date:** [December 26, 2023, 7:07pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/4 "2023-12-26T19:07:56Z")

</div>

Hey @hborla, I am aware that same-element requirements is not implemented yet. But once it is, would it be possible to require all elements of a pack to be of the same type without caring which type it is? For example:

```swift
struct Container<each S: Sequence> where repeat (each S).Element == ??

```

That is:

```swift
let numbers: [Int] = [1, 2, 3]
let bag: Set<Int> = [4, 5, 6]
// The following should be allowed
let containerA = Container(numbers, bag)

let names: [String] = ["Holly", "James", "Marcos"]
// The following won't compile because the elements are not of the same type
let containerB = Container(numbers, bag, names)

```

---

<div class="post-metadata">

**Author:** ![MPLewis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mplewis/32/40019_2.png) [@MPLewis](https://forums.swift.org/u/MPLewis)\
**Post date:** [December 26, 2023, 11:07pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/5 "2023-12-26T23:07:43Z")

</div>

From the linked proposal:

```swift
func variadic<each S: Sequence, T>(_: repeat each S) where repeat (each S).Element == T {}

```

You must make your type/function generic over the pack and the element type.

---

<div class="post-metadata">

**Author:** ![dehesa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dehesa/32/10911_2.png) [@dehesa](https://forums.swift.org/u/dehesa)\
**Post date:** [December 26, 2023, 11:15pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/6 "2023-12-26T23:15:42Z")

</div>

Right, I was missing the part of making the variadic type generic over the element type as well. Thank you for the reply.

```swift
struct Container<E, each S: Sequence> where repeat (each S).Element == E

```

---

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [January 26, 2024, 11:05pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/7 "2024-01-26T23:05:16Z")

</div>

@hborla By any chance were there plans to continue shipping [67465](https://github.com/apple/swift/pull/67465)? Or are there plans for a different workaround or solution to same element requirements? Thanks!

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [January 26, 2024, 11:06pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/8 "2024-01-26T23:06:24Z")

</div>

> [@vanvoorden](#):
>
> @hborla By any chance were there plans to continue shipping [67465](https://github.com/apple/swift/pull/67465)? Or are there plans for a different workaround or solution to same element requirements?

My PR has been subsumed by [[Requirement Machine] Implement same-element requirements. by simanerush · Pull Request #70227 · apple/swift · GitHub](https://github.com/apple/swift/pull/70227) which is actively being worked on by @simanerush.

---

<div class="post-metadata">

**Author:** ![moyerr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moyerr/32/13602_2.png) [@moyerr](https://forums.swift.org/u/moyerr)\
**Post date:** [March 13, 2024, 7:06pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/9 "2024-03-13T19:06:37Z")

</div>

Thank you @hborla and @simanerush! I just hit this limitation today, and am happy to see it's actively being worked on 👏

---

<div class="post-metadata">

**Author:** ![fwcd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fwcd/32/15804_2.png) [@fwcd](https://forums.swift.org/u/fwcd)\
**Post date:** [July 17, 2024, 12:31pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/10 "2024-07-17T12:31:55Z")

</div>

Very cool to see progress on this. My use case is having a function with a variadic number of arguments of the same type, i.e. something that accepts

```swift
() -> Bool
(Bool) -> Bool
(Bool, Bool) -> Bool
...

```

What I would love to write would be

```swift
func f<each B>(_ g: (repeat each B) -> Bool)
where
  repeat each B == Bool { ... }

```

My current workaround is to have a protocol for values that can be converted to/from `Bool` and then to require each `B` to conform to this, but of course it would be nice to have the ability to express this directly (also since it would likely play better with type inference, `{ $0 }` currently cannot be inferred as `(Bool) -> Bool`)

---

<div class="post-metadata">

**Author:** ![toastersocks](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toastersocks/32/27695_2.png) [@toastersocks](https://forums.swift.org/u/toastersocks)\
**Post date:** [July 18, 2024, 5:25am UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/11 "2024-07-18T05:25:22Z")

</div>

If the values are all the same type, can you use non-generic variadic parameters?:

```swift
func f(_ g: (Bool...) -> Bool)

```

---

<div class="post-metadata">

**Author:** ![fwcd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fwcd/32/15804_2.png) [@fwcd](https://forums.swift.org/u/fwcd)\
**Post date:** [July 18, 2024, 9:21am UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/12 "2024-07-18T09:21:19Z")

</div>

Yes, that would be an option. I would like to statically determine the arity of `g` in the `f`'s body, though, which would require runtime reflection if I am not mistaken.

---

<div class="post-metadata">

**Author:** ![toastersocks](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toastersocks/32/27695_2.png) [@toastersocks](https://forums.swift.org/u/toastersocks)\
**Post date:** [July 18, 2024, 9:33am UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/13 "2024-07-18T09:33:38Z")

</div>

Oh, interesting. I didn't know that was possible with parameter packs

---

<div class="post-metadata">

**Author:** ![potmo](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@potmo](https://forums.swift.org/u/potmo)\
**Post date:** [December 17, 2024, 9:37pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/14 "2024-12-17T21:37:53Z")

</div>

Has anyone got this to work with latest xcode 16.2?  
I think I've set the experimental flag but It doesn't seem to make any difference.

```swift
let package = Package(name: "Test",
                      platforms: [.macOS(.v15)],
                      products: [
                          .library(name: "Test",
                                   targets: ["Test"]),
                      ],
                      targets: [
                          .target(name: "Test",
                                  swiftSettings: [
.enableExperimentalFeature("SameElementRequirements"),
                                  ]),
                      ])

```

```swift
swift --version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)

```

---

<div class="post-metadata">

**Author:** ![potmo](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@potmo](https://forums.swift.org/u/potmo)\
**Post date:** [March 16, 2025, 4:25pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/15 "2025-03-16T16:25:20Z")

</div>

I have this code that I assume should work but it crashes the compiler (Apple Swift version 6.2-dev (LLVM 162ee50b401fff2, Swift 57288d13c9f3c02) in xcode 16.3 beta 2)

I'm trying to restrict the PropertyType of PropertyAnimatorTuple to be the same as all its elements T. Slight variations of the code gives different compiler errors but the one below crashes the compiler. @simanerush @hborla is this supposed to work?

```swift

public protocol TypedAnimateable {
}

extension Int: TypedAnimateable {
}

struct IntProp: PropertyAnimator {
    typealias PropertyType = Int
}

struct OtherIntProp: PropertyAnimator {
    typealias PropertyType = Int
}

public struct PropertyAnimatorTuple<PropertyType: TypedAnimateable, each T: PropertyAnimator>: PropertyAnimator
    where repeat (each T).PropertyType == PropertyType {
        public typealias PropertyType = PropertyType

        let theValue: (repeat each T)
        init(_ value: repeat each T) {
            // self.theValue = (repeat each value)
            fatalError("the line above also doesnt work no matter where I put the parenthesis")
        }
    }

func tester() {
  // let prop = PropertyAnimatorTuple<Int, IntProp, OtherIntProp>(IntProp(), OtherIntProp()) // this doesnt work. The compiler compains that "Int" and "Int, Int" is not equal.
    let prop: PropertyAnimatorTuple<Int, IntProp, OtherIntProp> = propThing(IntProp(), OtherIntProp())
}

 func propThing<each T: PropertyAnimator, PropertyType: TypedAnimateable>(_ value: (repeat each T)) where repeat (each T).PropertyType == PropertyType -> PropertyAnimatorTuple<PropertyType, each T> {
 return PropertyAnimatorTuple<PropertyType,each T>(repeat each value)
 }

```

---

<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:** [March 16, 2025, 4:44pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/16 "2025-03-16T16:44:22Z")

</div>

So far the experimental feature allows same-element requirements to be stated in generic signatures but some more plumbing is needed before more complex examples can work.

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [March 16, 2025, 10:07pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/17 "2025-03-16T22:07:42Z")

</div>

I’ve tried to use parameters packs a couple of times. I was afraid they might be a bit quirky, but I actually like the design a lot.

However, every attempt has ended up stymied by the lack of same-type constraints. I get the impression that most real-world use cases will face a similar block.

For example, there are several obvious applications within the swift-algorithms library, but they’d all require same-type constraints. I would hazard a guess that this is the reason those APIs haven’t already been converted to use packs.

Anyway, not a complaint! Just fan mail from someone who sees huge potential in packs and who’s eagerly awaiting the day when they reach their potential.

---

<div class="post-metadata">

**Author:** ![potmo](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@potmo](https://forums.swift.org/u/potmo)\
**Post date:** [March 25, 2025, 9:33pm UTC](https://forums.swift.org/t/variadic-types-same-element-requirements-are-not-yet-supported/67285/18 "2025-03-25T21:33:26Z")

</div>

I have hopes that this is being worked at since for example ([Pitch: User-defined tuple conformances](https://forums.swift.org/t/pitch-user-defined-tuple-conformances/67154)) depends on same-element requirements. Its a really powerful feature so I'm really looking forward to it.
