Is it possible to iterate over the arguments of a Swift parameter pack?

546 Views Asked by At

Parameter packs are a cool new feature of Swift 5.9, but surprisingly there doesn't seem to be a lot of discussion about them, and I hadn't seen this question answered on StackOverflow.

1

There are 1 best solutions below

3
On BEST ANSWER

Since I hadn't seen this question answered, I thought it would be useful to demonstrate how to do this. Swift 5.9 has no built-in way to iterate over parameter pack arguments, but we can do it as a side-effect of creating a tuple, discarding the tuple result if we don't need it.

func count<each T>(args: repeat each T) -> Int {
  var count = 0
  _ = (repeat (each args, count += 1))
  return count
}

The function above counts its arguments. Not terribly useful, but if we modify it just a bit, we get a function that can count the number of elements in any tuple.

func count<each T>(tuple: (repeat each T)) -> Int {
  var count = 0
  _ = (repeat (each tuple, count += 1))
  return count
}

There are all sorts of more advanced things we can do with this technique, often using helper functions or closures. For example, here is code from a parser combinator package I've written in Swift.

private func tupleHelper<C: Collection, A>(
  _ parser: Parser<C, A>,
  _ source: C,
  _ range: inout Range<C.Index>
) throws -> A {
  switch parser(source, at: range.upperBound) {
  case .success(let state):
    range = state.range
    return state.output
  case .failure(let error):
    range = error.index..<error.index
    throw error
  }
}

public func tuple<C: Collection, each A>(
  _ parser: repeat Parser<C, each A>
) -> Parser<C, (repeat each A)> {
  .init { source, index in
    var range: Range<C.Index> = index..<index
    do {
      // This is where the magic happens.
      let result = try (repeat tupleHelper(each parser, source, &range))
      return .success(.init(output: result, range: index..<range.upperBound))
    } catch let error as ParseError<C> {
      return .failure(error)
    } catch {
      return .failure(.init(reason: .error(error), index: range.lowerBound))
    }
  }
}

This combinator takes a variadic argument list of parsers with potentially different output types and returns them as a tuple. As with any parser combinator framework, the next parser starts parsing where the previous parser left off (assuming it succeeds). The helper function modifies the range using an inout parameter and keeps things moving along.

The main point is that when Swift "executes" the parameter pack, it calls each element in order, and we can take advantage of that to simulate iteration.