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

**URL:** <https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439>\
**Category:** Using Swift\
**Created:** [June 13, 2024, 9:41am UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439 "2024-06-13T09:41:16Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [June 13, 2024, 9:41am UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/1 "2024-06-13T09:41:16Z")

</div>

(I'm using Xcode 16 beta on macOS 14.5)

How do I get this to compile for both macOS 15 **and macOS 14**? :

```swift
public final class EmitterOfEvents {
	public typealias Element = Int
	public func notifications() -> some AsyncSequence<Element, Never> {
		AsyncStream<Element>.makeStream().stream
	}
}

```

I get error:

 ![Screenshot 2024-06-13 at 11.02.34](https://global.discourse-cdn.com/swift/original/3X/7/5/75536a33d734870fcb764663db3346e3d8db1360.png)

I'm well aware of [SE-0421](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0421-generalize-async-sequence.md), and I'm excited about the change. Hopefully I'm just a bit tired right now and what I want is possible, but it feels like something is missing?

I cannot use:

```swift
	@available(macOS 15.0, *)
	public func notifications() -> some AsyncSequence<Element, Never> {
		AsyncStream<Element>.makeStream().stream
	}

```

Since the method `notifications()` must exist and work on macOS 14 too.

I cannot use ` if #available(macOS 15.0, *) {` since that is put _inside the body_, and my problem is the return **type**...

I try:

```swift
#if swift(>=6.0)
	public typealias Notifications = AsyncSequence<Element, Never>
#else
	public typealias Notifications = AsyncSequence<Element>
#endif

```

That does not work either:

 ![Screenshot 2024-06-13 at 11.13.45](https://global.discourse-cdn.com/swift/original/3X/2/1/2116b64054941637cad3a3ef62057166d4f30796.png)

So Swift version is the "wrong dimension".

I guess I want `#if os(macOS 15.0, *)`, which is [discussed here](https://forums.swift.org/t/do-we-need-something-like-if-available/40349/25?page=2), but that does not exist.

So I try to find some macOS 15 only new SDK but could not find any... so I cannot use the `canImport(NewFancyMacOS15OnlyKit)` "trick" either. hmm...

This is in an SPM proj btw, can I do something clever in Package.swift, to create some macOS version dependent variable?

So can I use `@available` and create two different versions of `notifications()` with different return types? Hmm does not seem to be possible to spell "else" for `@available` ?

So what to do? 🤷‍♂️ hopefully I've missed something :)

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [June 13, 2024, 3:58pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/2 "2024-06-13T15:58:11Z")

</div>

[Existing (but unanswered) topic on this subject](https://forums.swift.org/t/conditionalising-function-declaration-on-os-version/72378).

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [June 13, 2024, 7:50pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/3 "2024-06-13T19:50:07Z")

</div>

Might `#if compiler` work? 🧐 will try tommorow

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [June 27, 2024, 2:32pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/4 "2024-06-27T14:32:38Z")

</div>

Have you been able to resolve this issue?

---

<div class="post-metadata">

**Author:** ![0xfeedface1993](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xfeedface1993/32/36042_2.png) [@0xfeedface1993](https://forums.swift.org/u/0xfeedface1993)\
**Post date:** [June 27, 2024, 3:05pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/5 "2024-06-27T15:05:12Z")

</div>

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.

```swift
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
    }
}

```

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [June 27, 2024, 3:11pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/6 "2024-06-27T15:11:09Z")

</div>

Since I have it in protocol, I've managed to workaround it by just constraining `Element` as preserving this type was the most important part:

```swift
#if swift(>=6.0)
    associatedtype ChangesStream: AsyncSequence<Change, Never>
#else
    associatedtype ChangesStream: AsyncSequence where ChangesStream.Element == Change
#endif

```

With some ugly generic on implementation side in that case (yet not that critical) it is working and keeping type information, while still forcing to catch error at usage side.

Anyway, some `#if available` could've been useful for such cases, especially given that on Apple platforms that happens a lot.

---

<div class="post-metadata">

**Author:** ![0xfeedface1993](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xfeedface1993/32/36042_2.png) [@0xfeedface1993](https://forums.swift.org/u/0xfeedface1993)\
**Post date:** [June 27, 2024, 3:23pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/7 "2024-06-27T15:23:01Z")

</div>

The `AsyncExtensions` library, which is based on `AsyncSequence`, encountered a similar issue. The solution also involved using `associatedtype` and protocol splitting, allowing both the `Element` and `Failure` generics to function properly. You can refer to it here:

[AsyncSubject.swift](https://github.com/sideeffect-io/AsyncExtensions/blob/main/Sources/AsyncSubjects/AsyncSubject.swift)

If you want to implement advanced features like `#if available`, I recommend using `Swift Macro`. It allows for very complex code processing but requires more sophisticated programming.

---

<div class="post-metadata">

**Author:** ![NeonTetra](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neontetra/32/28331_2.png) [@NeonTetra](https://forums.swift.org/u/NeonTetra)\
**Post date:** [June 27, 2024, 11:49pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/8 "2024-06-27T23:49:11Z")

</div>

When using protocol spliting, or declaring `Failure` as new associated type or type ailas cause protocol witness table crash in runtime which is before swift 6.  
So you shouldn't reference the `Failure` type directly when you actually run in before swift 6 runtime.

Below code do compile, but using it will cause `abort` or `protocol witness table crash`.

```swift
public protocol TypedAsyncIteratorProtocol<Element, Err>: ~Copyable {
    
    associatedtype Element
    associatedtype Err: Error
    
    // hack for compiler but crash on runtime, you have to remove this line or Crash!
    typealias Failure = Err
    
    @inlinable
    mutating func next(isolation actor: isolated (any Actor)?) async throws(Err) -> Element?

}

public protocol TypedAsyncSequence<Element, Err>:AsyncSequence where AsyncIterator: TypedAsyncIteratorProtocol{

    /// The type of errors produced when iteration over the sequence fails.
    associatedtype Err = AsyncIterator.Err where Err == AsyncIterator.Err

    /// Creates the asynchronous iterator that produces elements of this
    /// asynchronous sequence.
    ///
    /// - Returns: An instance of the `AsyncIterator` type used to produce
    /// elements of the asynchronous sequence.
    @inlinable
    func makeAsyncIterator() -> AsyncIterator
}

```

I think these issue has some relation to this kind of thing.  
[AsyncIterator Failure crash](https://github.com/swiftlang/swift/issues/74292)

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [June 28, 2024, 6:32am UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/9 "2024-06-28T06:32:20Z")

</div>

Are you trying to make this code compile with multiple versions of the compiler/SDK, or are you trying to get the snippet to work with the Swift 6 compiler but a deployment target lower than macOS 15?

The main problem with the code as written is that it specifies a `some AsyncSequence` type. `AsyncSequence` only started specifying primary associated types `Element` and `Failure` in the Swift 6 standard library and therefore you can only spell a `some AsyncSequence` type in code that both compiles with a Swift 6 compiler and runs on OSes aligned with Swift 6 or later.

If your code needs to be backward compatible with either older SDKs or older runtimes, then refactoring the code to use a concrete type conforming to `AsyncSequence` instead is your best bet I think:

```swift
public struct EventSequence<Element>: AsyncSequence {
  let underlyingStream: AsyncStream<Element>

  public func makeAsyncIterator() -> AsyncStream<Element>.AsyncIterator {
    underlyingStream.makeAsyncIterator()
  }
}

public final class EmitterOfEvents {
  public typealias Element = Int

  // no need for @available or #if
  public func notifications() -> EventSequence<Element> {
    let stream = AsyncStream<Element>.makeStream().stream
    return EventSequence(underlyingStream: stream)
  }
}

```

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [June 28, 2024, 7:49am UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/10 "2024-06-28T07:49:31Z")

</div>

> [@tshortli](#):
>
> Are you trying to make this code compile with multiple versions of the compiler/SDK, or are you trying to get the snippet to work with the Swift 6 compiler but a deployment target lower than macOS 15?

The latter. Which I think is situation many Swift devs are in: Not ready to install macOS 15 beta, but keen on using Xcode 16 beta.

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [June 28, 2024, 3:59pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/11 "2024-06-28T15:59:29Z")

</div>

Ok, code that runs on a deployment target lower than macOS 15 can’t use a `some AsyncSequence` type.

---

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 8, 2024, 10:35pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/12 "2024-07-08T22:35:13Z")

</div>

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

```swift
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`?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [July 8, 2024, 11:34pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/13 "2024-07-08T23:34:17Z")

</div>

> [@0xfeedface1993](#):
>
> 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.

I would avoid this trick if possible. Consider this example:

```swift
public protocol P1 {
  //associatedtype Failure
}

public protocol P2 {
  associatedtype Failure
}

public func sameTypeRequirement<T: P1 & P2>(_: T)
    where T.Failure == Never {}

```

The mangled name of `sameTypeRequirement()` is

```swift
s1u19sameTypeRequirementyyxAA2P1RzAA2P2Rzs5NeverO7FailureAaDPRtzlF

```

However, if you uncomment the associated type declaration in P1, it changes to

```swift
s1u19sameTypeRequirementyyxAA2P1RzAA2P2Rzs5NeverO7FailureAaCPRtzlF

```

Now imagine that P1 is AsyncSequence and P2 is your protocol. Building the code on the old SDK vs new SDK will change the mangled name of our function, which is a problem if you are building a shared library that you intend on distributing. (EDIT: Unless `(<<your module name>>, P2)` follows `(_Concurrency, AsyncSequence)` in lexicographic order; then the mangled name won't change).

---

<div class="post-metadata">

**Author:** ![kabiroberai](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kabiroberai/32/28072_2.png) [@kabiroberai](https://forums.swift.org/u/kabiroberai)\
**Post date:** [December 19, 2024, 8:14pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/14 "2024-12-19T20:14:24Z")

</div>

Just want to add that I was experimenting with this and found a few other instances where this leads to trouble at runtime:

```swift
protocol P1 {
    associatedtype Base: AsyncSequence
    @available(iOS 18.0, *)
    associatedtype _Failure = Base.Failure
}
public protocol P2 {
    associatedtype _Failure
    associatedtype Failure = _Failure
}
enum AsyncSequenceTraits<Base: AsyncSequence>: P1, P2 {}
typealias FailureOf<S: AsyncSequence> = AsyncSequenceTraits<S>.Failure

```

This makes it _ostensibly_ possible to replace any usage of `S.Failure` with `FailureOf<S>` — but the kicker is that it allows for constructs such as:

```swift
func extractFailureType(of sequence: any AsyncSequence) -> Error.Type {
    func helper<S: AsyncSequence>(_ sequence: S) -> Error.Type {
        FailureOf<S>.self
    }
    return helper(sequence)
}

```

which is entirely unsafe on Darwin prior to the Fall2024 OSes.

FWIW I did initially think we'd be safe as long as we stuck to using this with `where` constraints, but anecdotally I've also seen crashes in code like the following (could be related to the lexicographical ordering snag that Slava mentioned?)

```swift
func doSomething<S: AsyncSequence>(_ sequence: S) where FailureOf<S> == Never {
  ...
}

```

In fact, I was able to simplify the two protocols into one:

```swift
protocol P1 {
    associatedtype Base: AsyncSequence
    associatedtype Failure

    @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
    associatedtype _Witness = Never where Base.Failure == Failure
}
enum AsyncSequenceTraits<Base: AsyncSequence>: P1 {}
typealias FailureOf<S: AsyncSequence> = AsyncSequenceTraits<S>.Failure

```

Which I've attached as an MRE to the GitHub issue mentioned some posts up: [Swift 6 runtime/Compile Crash with AsyncIteratorProcotol TypedThrow · Issue #74292 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/issues/74292) (though I can create a separate issue if it would help with tracking.)

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 19, 2024, 9:31pm UTC](https://forums.swift.org/t/how-to-use-asyncsequence-on-macos-14-5-in-xcode-16-beta-need-help-with-availability-check-since-failure-is-unavailb-e/72439/15 "2024-12-19T21:31:29Z")

</div>

> [@kabiroberai](#):
>
> Just want to add that I was experimenting with this and found a few other instances where this leads to trouble at runtime:
> 
> ```swift
> protocol P1 {
> associatedtype Base: AsyncSequence
> @available(iOS 18.0, *)
> associatedtype _Failure = Base.Failure
> }
> public protocol P2 {
> associatedtype _Failure
> associatedtype Failure = _Failure
> }
> enum AsyncSequenceTraits<Base: AsyncSequence>: P1, P2 {}
> typealias FailureOf<S: AsyncSequence> = AsyncSequenceTraits<S>.Failure
> 
> ```

In fact this is a bug in availability checking. To see why, note that the type `AsyncSequenceTraits<S>.Failure` resolves to a type alias member of `AsyncSequenceTraits`. This type alias is synthesized when we check the conformance of `AsyncSequenceTraits` to `P1` or `P2`. The underlying type of this type alias is always going to be `Base.Failure`, so we can replace `AsyncSequenceTraits<S>.Failure` with `S.Failure`. There are no other usages of `P1` and `P2`, so they can be removed.

If you do that though, you get an availability error as expected:

```swift
typealias FailureOf<S: AsyncSequence> = S.Failure

```

To see that there is no other difference, take your original `FailureOf` and try compiling this program with `-emit-silgen`:

```swift
func f<S: AsyncSequence>(_: FailureOf<S>, _: S) {}

```

You'll see that the parameter type in both cases is really just `S.Failure`:

```swift
sil hidden [ossa] @$s1a1fyy7FailureQz_xtSciRzlF : $@convention(thin) <S where S : AsyncSequence> (@in_guaranteed S.Failure, @in_guaranteed S) -> () {

```

That is to say, it's all ultimately boils down to:

```swift
func f<S: AsyncSequence>(_: S.Failure, _: S) {}

```
