# SE-0421: Generalize effect polymorphism for AsyncSequence and AsyncIteratorProtocol

**URL:** <https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662>\
**Category:** Proposal Reviews\
**Created:** [January 26, 2024, 11:08pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662 "2024-01-26T23:08:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [January 26, 2024, 11:08pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/1 "2024-01-26T23:08:59Z")

</div>

Hello Swift community,

The review of [SE-0421: Generalize effect polymorphism for `AsyncSequence` and `AsyncIteratorProtocol`](https://github.com/apple/swift-evolution/blob/main/proposals/0421-generalize-async-sequence.md) begins now and runs through February 7, 2024.

Reviews are an important part of the Swift evolution process. All review feedback should be either on this forum thread or, if you would like to keep your feedback private, [directly to me](mailto:fred.kl@me.com) as the review manager via the forum messaging feature. When contacting the review manager directly, please put "SE-0421" in the subject line.

##### Trying it out

If you'd like to try this proposal out, you can download a toolchain supporting it:

- macOS: [https://ci.swift.org/job/swift-PR-toolchain-macos/1039/artifact/branch-main/swift-PR-70635-1039-osx.tar.gz](https://ci.swift.org/job/swift-PR-toolchain-macos/1039/artifact/branch-main/swift-PR-70635-1039-osx.tar.gz)
- Linux: [https://download.swift.org/tmp/pull-request/70635/716/ubuntu2004/PR-ubuntu2004.tar.gz](https://download.swift.org/tmp/pull-request/70635/716/ubuntu2004/PR-ubuntu2004.tar.gz)
- Windows: [Artifacts of Swift Pull Request Toolchain Windows #1120 : / [Jenkins]](https://ci-external.swift.org/job/swift-PR-build-toolchain-windows/1120/artifact/)

##### What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

- What is your evaluation of the proposal?
- Is the problem being addressed significant enough to warrant a change to Swift?
- Does this proposal fit well with the feel and direction of Swift?
- If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
- How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at

> [https://github.com/apple/swift-evolution/blob/main/process.md](https://github.com/apple/swift-evolution/blob/main/process.md)

Thank you,

Freddy Kellison-Linn  
Review Manager

---

<div class="post-metadata">

**Author:** ![dmt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmt/32/30830_2.png) [@dmt](https://forums.swift.org/u/dmt)\
**Post date:** [January 26, 2024, 11:30pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/2 "2024-01-26T23:30:39Z")

</div>

As AsyncIteratorProtocol and AsyncSequence are declared in \_Concurrency couldn't we use BackDeploymentConcurrency on OSes with runtime up to 5.11 and thus get rid of the `@available` ?

---

<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:** [January 27, 2024, 12:15am UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/3 "2024-01-27T00:15:40Z")

</div>

Sounds great.

Thinking ahead a bit, what will happen for an `AsyncStream` conformer that implements back-pressure? i.e. doesn't necessarily sit waiting in the upstream `next` all the time; how will it support cancellation if it can no longer throw `CancellationError` (because the type is no longer necessarily the unlimited `any Error`)? Is it forced to just quietly return `nil` from its iterator's `next`?

---

<div class="post-metadata">

**Author:** ![aggie33](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aggie33/32/29896_2.png) [@aggie33](https://forums.swift.org/u/aggie33)\
**Post date:** [January 29, 2024, 1:45am UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/4 "2024-01-29T01:45:38Z")

</div>

Definitely +1 on this proposal. This will resolve a longstanding issue with `AsyncSequence` and finally make it more usable. One thing I wonder is with this proposal, what is the future of `@rethrows protocol`s? They were never officially put through Evolution, but they exist in the language today. Should we deprecate or remove them?

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [January 29, 2024, 11:07am UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/5 "2024-01-29T11:07:12Z")

</div>

> - What is your evaluation of the proposal?

+1. This proposal solves two of the existing problems with the `AsyncSequence` protocol in a pragmatic way.

> - Is the problem being addressed significant enough to warrant a change to Swift?

Yes this problem is very important to address and has made usage of `AsyncSequence`s hard or impossible without introducing `Sendable` problems.

> - How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

I reviewed the proposal in-depth and worked extensively with `AsyncSequence`s in the past.

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [January 29, 2024, 11:08am UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/6 "2024-01-29T11:08:31Z")

</div>

> [@wadetregaskis](#):
>
> Thinking ahead a bit, what will happen for an `AsyncStream` conformer that implements back-pressure? i.e. doesn't necessarily sit waiting in the upstream `next` all the time; how will it support cancellation if it can no longer throw `CancellationError` (because the type is no longer necessarily the unlimited `any Error`)? Is it forced to just quietly return `nil` from its iterator's `next`?

`AsyncStream` and `AsyncThrowingStream` are currently both returning `nil` when they recognise that their consuming task was cancelled.

---

<div class="post-metadata">

**Author:** ![nonameplum](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nonameplum/32/6450_2.png) [@nonameplum](https://forums.swift.org/u/nonameplum)\
**Post date:** [January 29, 2024, 2:29pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/7 "2024-01-29T14:29:41Z")

</div>

> [@FranzBusch](#):
>
> `AsyncStream` and `AsyncThrowingStream` are currently both returning `nil` when they recognise that their consuming task was cancelled.

A bit of topic, but would it be worth it to create another proposal to allow the `AsyncStream` / `AsyncThrowingStream` to specify the cancellation behavior, e.g. by adding to the `init`:

```swift
init(
    _ elementType: Element.Type = Element.self,
    bufferingPolicy limit: AsyncStream<Element>.Continuation.BufferingPolicy = .unbounded,
    cancellationStrategy: CancellationStrategy = .immediate,
    _ build: (AsyncStream<Element>.Continuation) -> Void
)

public enum CancellationStrategy {
    case immediate
    case cooperative
}

```

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [January 29, 2024, 5:15pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/8 "2024-01-29T17:15:57Z")

</div>

> [@aggie33](#):
>
> One thing I wonder is with this proposal, what is the future of `@rethrows protocol`s? They were never officially put through Evolution, but they exist in the language today. Should we deprecate or remove them?

Yes, we should deprecate and remove them. Assuming this proposal (or something similar to it) goes through to obviate the need for `@rethrows`, we'll start staging them out soon.

Doug

---

<div class="post-metadata">

**Author:** ![Jason\_Gregori](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jason_gregori/32/15205_2.png) [@Jason\_Gregori](https://forums.swift.org/u/Jason_Gregori)\
**Post date:** [February 8, 2024, 3:54pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/9 "2024-02-08T15:54:51Z")

</div>

+1, great addition!

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [February 19, 2024, 7:04pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/10 "2024-02-19T19:04:09Z")

</div>

For completeness, I'd like to note in this thread that SE-0421 has been [accepted](https://forums.swift.org/t/accepted-se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69973). Thank you all for your feedback!

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [February 19, 2024, 7:04pm UTC](https://forums.swift.org/t/se-0421-generalize-effect-polymorphism-for-asyncsequence-and-asynciteratorprotocol/69662/11 "2024-02-19T19:04:15Z")

</div>

Closed on February 19, 2024, 7:04pm UTC


