Is there a way to spread a tuple into the arguments of a function?

I don't think anything better is or has ever been possible. So I just don't write anything with them.


You can make tuples out of sequences…

public extension Sequence {
  func tuplePrefix<each Element>() -> (repeat each Element) {
    var iterator = makeIterator()
    return (repeat iterator.next()! as! each Element)
  }
}
#expect((1...10).tuplePrefix() == (1, 2, 3)) // ✅

…and this will potentially get safer via Integer Generic Parameters.

But as far as I know, there's no way to pump expanded packs into variadic arguments…?

public func splat<Element, each TupleElement>(
  _ f: @escaping (Element...) -> Void
) -> ((repeat each TupleElement)) -> Void {
  // Error: Cannot pass value pack expansion to non-pack parameter of type 'Element'
  { return f(repeat each $0 as! Element) }
}

If that worked, you could do

splat(f)((1...10).tuplePrefix() as (Int, Int, Int))
1 Like