Generic parameter pack expansion only for return value

This thread is created on request of @scanon (Add splitMap function to Swift standard library - #10 by scanon)

The original intent is to address cases where single input Type is needed to be transformed into multiple Types, like this:

extension Sequence {
// curent imp:
  func splitMap<T1, T2, E: Error>(_ transform: (Element) throws(E) -> Either<T1, T2>) rethrows -> ([T1], [T2]) // we are currently limited by 2 generic Types

// what we need is something like:
  func splitMap<each T, E: Error>(_ transform: (Element) throws(E) -> OneOf<each T>) rethrows -> (repeat each Array<T>)
}

For now it is possible to implement a function with multiple input Types and single output Type by pack iteration which is already pitched and implemented.

func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each Element)) -> Bool {

So some solution is also needed for the specified case.

2 Likes

I don't have any good idea on this kind of things (maybe 'variadic sum type'?), but this post seemed to be facing somehow related issue.

Yes, thanks for handling this. I suppose the are needed to me merged in one thread.

You can poorly implement a variadic sum type with repeat Optional<each T>, which might be sufficient for something like this where you hope it all gets inlined away. I don’t think we’d put that in the stdlib though.

3 Likes