Possible to return an AsyncSequence<T> for iOS 17 when building against iOS 18?

AsyncSequence got changed in iOS 18, and now I can't figure out how to use it as a return type in a way that works with both 17 and 18.

Example:

func subscribe() -> any AsyncSequence<String>  { ... }

Gives Error: Protocol type 'AsyncSequence' specialized with too few type arguments (got 1, but expected 2)

But this:

func subscribe() -> any AsyncSequence<String, Never> { ... }

Gives Error: 'Failure' is only available in iOS 18.0 or newer

Which I can fix by adding in @available(iOS 18.0, *) -- but now I am back to having no iOS 17 API.

... I feel like there has to be an obvious, simple answer that I am just not seeing?

1 Like

There might be some ideas here that could help.

1 Like

Did you solve this? Or is it impossible to use AsyncSequence<SomeType> as a return type for apps targeting below iOS 18?

No, sadly, the type erasure to any AsyncSequence needs the PAT for Failure introduced in iOS 18, macOS 15, etc.

For now, it’s easiest to use the concrete types, perhaps using typealias to simplify its usage.

1 Like

I'm still using this for a workaround. Instead of returning an opaque (or erased) AsyncSequence… my idea here is to box a concrete AsyncSequence in an opaque Container. The extra layer of indirection seems to help keep the compiler happy.

1 Like

Nope. :frowning_face:

It is impossible to use AsyncSequence<SomeType> as a return type for code targeting support below iOS 18.

So I guess this also means it's impossible to use a typed AsyncSequence as a return type in general pre ios 18?

You can use the concrete type of the sequence in question, you just lose type info if you make it opaque.

Already shared this with @joshw on Slack (we work together) but for visibility I thought I'd point out that swift-nio has a nice AnyAsyncSequence type that can do the trick. Though folks have pointed out in the past that this can have sizable perf tradeoffs so probably best used sparingly.

1 Like