An opaque parameter (a some parameter) is syntactic sugar for a generic parameter. For example, this function with an opaque parameter:
func map<A, B>(xs: some Sequence<A>, _ f: (A) -> B) -> some Sequence<B> {
return xs.map(f)
}
is syntactic sugar for this function with an additional generic parameter:
func map<A, B, S>(xs: S, _ f: (A) -> B) -> some Sequence<B>
where S: Sequence, S.Element == A
{
return xs.map(f)
}
Now let's look at your function:
If we try to de-sugar the opaque parameter, we end up with something like this:
func map<A, B>(_ f: @escaping (A) -> B)
-> (<S> (S) -> some Sequence<B> where S: Sequence, S.Element == A)
{
return { xs in xs.map(f) }
}
Which is to say, your map function wants to return a generic function.
Unfortunately, in Swift, generic functions are not “first-class”: you cannot treat a generic function as a value. For example, you can't do this:
func identity<A>(_ a: A) -> A { a }
let id = identity // error: Generic parameter 'A' could not be inferred
You can only turn a generic function into a value if you provide all the generic parameters, thus producing a non-generic function:
func identity<A>(_ a: A) -> A { a }
let id_Int = identity as (Int) -> _
Since you can't treat a generic function as a value, you can't return a generic function.
As for how you can solve your problem, it depends. Why are you trying to return a generic function?
One solution, which may or may not be applicable to your problem, is to return a value of some type that has a generic function. For example:
struct Mapper<A, B> {
let f: (A) -> B
func callAsFunction(xs: some Sequence<A>) -> some Sequence<B> {
return xs.map(f)
}
}
func map<A, B>(_ f: @escaping (A) -> B) -> Mapper<A, B> { .init(f: f) }