How do I call `each` closure of a parameter pack saved to an instance variable from outside that type?

Hi! I am experimenting with parameter packs and seeing a confusing error from Swift 5.9.2. Can anyone help me understand what is wrong about this code?

I create a ClosureRepeater type that saves a parameter pack of closures as an instance variable. I seem to have no problem calling each closure from inside my type… but I seem to be unable to call each closure from outside that type:

struct ClosureRepeater<each Element: Equatable> {
  let closureTuple: (repeat () -> each Element)
  
  init(_ tuple: (repeat () -> each Element)) {
    self.closureTuple = tuple
  }
  
  var elementTuple: (repeat each Element) {
    (repeat (each self.closureTuple)())         //  [0]
  }
}

let repeater = ClosureRepeater(({ 1 }, { "1" }))

let _ = repeater.elementTuple                   //  [1]

let _ = (repeat (each repeater.closureTuple)()) //  [2]: 'each' cannot be applied to non-pack type '(() -> Int, () -> String)'

Does anyone know why the code from line [0] and [1] seems to be compiling correctly… but the code from line [2] seems to break? Does anyone know the workaround or the correct syntax to call every closure of a pack that was saved as an instance variable?

I have not found an issue like this being tracked… was this already part of another fix that landed in latest main?

Any advice for that would be a great big help. Thanks!

You can't use each with a concrete tuple type; it only works on variadic tuples in generic context.

2 Likes

@John_McCall ahh… interesting! thanks!