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

This seems to compile from 6.0 and deploys (runs) on macOS 14.

public protocol EmitterProtocol<Element, Failure> {
  associatedtype Element where Self.Sequence.Element == Element
  @available(macOS 15.0, *) associatedtype Failure : Error = any Error where Self.Sequence.Failure == Failure
  associatedtype Sequence : AsyncSequence
  
  func notifications() -> Sequence
}

public final class Emitter : EmitterProtocol {
  let (stream, continuation) = AsyncStream<Int>.makeStream()
  
  public func notifications() -> AsyncStream<Int> {
    self.stream
  }
}

public final class Listener {
  public func listen(to emitter: some EmitterProtocol) async throws {
    try await self.listen(to: emitter.notifications())
  }
  
  func listen<S>(to sequence: S) async throws where S : AsyncSequence {
    for try await value in sequence {
      print(value)
    }
  }
}

func main() async throws {
  let emitter = Emitter()
  emitter.continuation.yield(1)
  emitter.continuation.yield(2)
  emitter.continuation.yield(3)
  emitter.continuation.finish()
  
  let listener = Listener()
  try await listener.listen(to: emitter)
}

try await main()

Maybe a workaround from a different POV is to not think about returning an opaque sequence type from a concrete Emitter… what if we return a concrete sequence type from an opaque Emitter?

1 Like