How to use `AsyncSequence` on macOS 14.5 in Xcode 16 beta? Need help with availability check since `Failure` is unavailb.e

Please try the following solution:

macOS 15's AsyncSequence defines a Failure type, which is not available in earlier versions.

Therefore, we utilize the associatedtype keyword and create a new protocol FailureableAsyncSequence that associates a new Failure type. We make AsyncStream conform to both protocols.

When the AsyncSequence in older systems does not define Failure , it will automatically associate with the Failure type from our new protocol.

Although this does not perfectly solve your problem, it is a feasible solution.

public protocol SomeAsyncSequence: AsyncSequence {
    
}

public protocol FailureableAsyncSequence {
    associatedtype Failure: Error
}

public typealias CustomAsyncSequence = SomeAsyncSequence & FailureableAsyncSequence

extension AsyncStream: CustomAsyncSequence where Element == Int {
    public typealias Failure = Never
}

public final class EmitterOfEvents {
    public typealias Element = Int
    public func notifications() -> some CustomAsyncSequence {
        AsyncStream<Element>.makeStream().stream
    }
}
1 Like