Can we mutate through variadic generic pack iteration?

I saw this in Swift's blog, in an article about pack iteration:

func allEmpty<each T>(_ array: repeat [each T]) -> Bool {
  for a in repeat each array {
    guard a.isEmpty else { return false }
  }

  return true
}

These just read (parts of) the arguments. What if I wanted to mutate each variadic member? Could I use "a"? What about using "array" within the block?

My actual example using an iterator (IteratorProtocol) storing several iterators. The initializer uses the pack for generating each parameter. The internal storage uses the pack to define members of a tuple. So I need the top-layer "next" to call all the inner ones.

Mutation of packs is not supported yet. For now, you can work around this with something like

func f<T: P>(_ value: T) -> T {
  var copy = value
  copy.someMutatingMethod()
  return copy
}

foo = (repeat f(each foo))
2 Likes