# Language limitation, or am I not doing this right? \`Error: Pack expansion requires that 'each Element' and 'repeat each Element' have the same shape\`

**URL:** <https://forums.swift.org/t/language-limitation-or-am-i-not-doing-this-right-error-pack-expansion-requires-that-each-element-and-repeat-each-element-have-the-same-shape/75936>\
**Category:** Using Swift\
**Tags:** generics, variadic-generics, parameter-packs\
**Created:** [November 10, 2024, 5:11pm UTC](https://forums.swift.org/t/language-limitation-or-am-i-not-doing-this-right-error-pack-expansion-requires-that-each-element-and-repeat-each-element-have-the-same-shape/75936 "2024-11-10T17:11:03Z")\
**Posts on this page:** 4\
**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:** [November 10, 2024, 5:11pm UTC](https://forums.swift.org/t/language-limitation-or-am-i-not-doing-this-right-error-pack-expansion-requires-that-each-element-and-repeat-each-element-have-the-same-shape/75936/1 "2024-11-10T17:11:03Z")

</div>

I'm trying to write a variadic zip function that will take a function `A... -> B`, and returns a function `Functor<A>... -> Functor<B>`. I feel like I'm close, but am running into an error and I'm not sure if this is just a current limitation of the language, or if I'm doing something wrong:

```swift
struct Functor<A> {
    let work: () -> A

    func map<B>(_ transform: @escaping (A) -> B) -> Functor<B> {
        Functor<B> { transform(work()) }
    }
}

func zip<each Element>(_ functor: repeat Functor<each Element>) -> Functor<(repeat each Element)> {
    Functor<(repeat each Element)> {
        (repeat (each functor).work())
    }
}
// So far so good

// Here comes trouble...
func zip<each Element, Result>(with transform: @escaping ((repeat each Element)) -> Result) -> (repeat Functor<each Element>) -> Functor<Result> {
    func resultFunc(_ element: repeat Functor<each Element>) -> Functor<Result> {
        zip(repeat each element).map(transform)
    }
    
    return resultFunc // Error: Pack expansion requires that 'each Element' and 'repeat each Element' have the same shape
}

```

To simplify the example I tried

```swift
let foo = resultFunc

```

And it's inferred to be of type

```swift
(repeat Functor<each Element>) -> Functor<Result>

```

which on the surface, seems to be exactly what I want, but when I explicitly specify the type to be the same as what's inferred

```swift
let foo: (repeat Functor<each Element>) -> Functor<Result> = resultFunc 
// Error: Pack expansion requires that 'each Element' and 'repeat each Element' have the same shape

```

I get the same error.  
Is this an inherent limitation of the language right now, or am I not specifying my types correctly? Any way to do what I'm wanting?

> **What I'm trying to avoid**
>
> ```swift
> func zip2<A, B, C>(
> with transform: @escaping (A, B) -> C
> ) -> (Functor<A>, Functor<B>) -> Functor<C> {
> { zip2($0, $1).map(transform) }
> }
> func zip3<A, B, C, D>(
> with transform: @escaping (A, B, C) -> D
> ) -> (Functor<A>, Functor<B>, Functor<C>) -> Functor<D> {
> { zip3($0, $1, $2).map(transform) }
> }
> // etc... up to Nth arity 
> 
> ```

---

<div class="post-metadata">

**Author:** ![mtsrodrigues](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mtsrodrigues/32/34169_2.png) [@mtsrodrigues](https://forums.swift.org/u/mtsrodrigues)\
**Post date:** [November 10, 2024, 7:14pm UTC](https://forums.swift.org/t/language-limitation-or-am-i-not-doing-this-right-error-pack-expansion-requires-that-each-element-and-repeat-each-element-have-the-same-shape/75936/2 "2024-11-10T19:14:25Z")

</div>

Is this what you want?

```swift
func zip<each Element, Result>(transform: @escaping (repeat each Element) -> Result) -> (repeat Functor<each Element>) -> Functor<Result> {
    { (functors: repeat Functor<each Element>) in
        return Functor<Result> { transform(repeat (each functors).work()) }
    }
}

let result = zip(transform: { (a: Int, b: Int, c: String) in "" })

print(type(of: result)) 
// (Functor<Int>, Functor<Int>, Functor<String>) -> Functor<String>

```

---

<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:** [November 10, 2024, 9:48pm UTC](https://forums.swift.org/t/language-limitation-or-am-i-not-doing-this-right-error-pack-expansion-requires-that-each-element-and-repeat-each-element-have-the-same-shape/75936/3 "2024-11-10T21:48:06Z")

</div>

Oh my gosh yes, that compiles! I didn't think it was possible with the closure so I was making that inner func. Thank you! Still curious why the types weren't matching though.  
I think I can make this work, but there's an odd difference when I use it as compared with the non-variadic versions.  
With the non-variadic versions, I could do this:

```swift
func boo(a: Int, b: Double, c: String) -> String {
    "\(a), \(b), \(c)"
}

let functOfBoo = zip3(with: boo)(
    Functor { 4 }, 
    Functor { 4.0 }, 
    Functor { "4" } 
) 

```

But with the variadic version:

```swift
func boo(a: Int, b: Double, c: String) -> String {
    "\(a), \(b), \(c)"
}

let functOfBoo = zip(with: boo)(
    Functor { 4 }, // Error: Cannot pass value pack expansion to non-pack parameter of type 'Functor<Int>'
    Functor { 4.0 }, // Error: Cannot pass value pack expansion to non-pack parameter of type 'Functor<Int>'
    Functor { "4" } // Error: Cannot pass value pack expansion to non-pack parameter of type 'Functor<Int>'
) // The same error is shown for all three parameters, always referring to whatever type was in first position

```

However I can make the variadic version work by separating the calls, assigning the result function and calling it into two steps:

```swift
func boo(a: Int, b: Double, c: String) -> String {
    "\(a), \(b), \(c)"
}

let booFunctFactory = zip(with: boo)
let functOfBoo = booFunctFactory(
    Functor { 4 }, 
    Functor { 4.0 }, 
    Functor { "4" } 
) 
// This works

```

I can make this work, but I'm curious about why this is needed and if there's a way to recover the easier use at the callsite?

---

<div class="post-metadata">

**Author:** ![mtsrodrigues](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mtsrodrigues/32/34169_2.png) [@mtsrodrigues](https://forums.swift.org/u/mtsrodrigues)\
**Post date:** [November 11, 2024, 3:59pm UTC](https://forums.swift.org/t/language-limitation-or-am-i-not-doing-this-right-error-pack-expansion-requires-that-each-element-and-repeat-each-element-have-the-same-shape/75936/4 "2024-11-11T15:59:23Z")

</div>

> [@toastersocks](#):
>
> ```swift
> let functOfBoo = zip(with: boo)(
> Functor { 4 }, // Error: Cannot pass value pack expansion to non-pack parameter of type 'Functor<Int>'
> Functor { 4.0 }, // Error: Cannot pass value pack expansion to non-pack parameter of type 'Functor<Int>'
> Functor { "4" } // Error: Cannot pass value pack expansion to non-pack parameter of type 'Functor<Int>'
> ) // The same error is shown for all three parameters, always referring to whatever type was in first position
> 
> ```

I would expect this to work 🤔 @hborla could you take a look here, please? Is this intended or something that the compiler should be able to handle?
