# Missing type erasure?

**URL:** <https://forums.swift.org/t/missing-type-erasure/61377>\
**Category:** Swift Async Algorithms\
**Created:** [November 11, 2022, 10:20am UTC](https://forums.swift.org/t/missing-type-erasure/61377 "2022-11-11T10:20:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 11, 2022, 10:20am UTC](https://forums.swift.org/t/missing-type-erasure/61377/1 "2022-11-11T10:20:39Z")

</div>

While working on some streams, I realized that the current APIs are missing any type of type erasure for the streams. The "ideal" solution would be to have `some AsyncSequence<Value, Failure>`, but that's a bit far away as the language is missing typed throws which would be required to properly expose the `Failure` type through the protocol. On the other hand `Combine` already has an `eraseToAnyPublisher()` method for that purpose.

In my particular case I had to write the following extension for convenience purposes.

```swift
extension AsyncSequence {
  func eraseToAsyncThrowingStream() -> AsyncThrowingStream<Element, Error> {
    AsyncThrowingStream { continuation in
      Task {
        do {
          for try await value in self {
            continuation.yield(value)
          }
          continuation.finish()
        } catch {
          continuation.finish(throwing: error)
        }
      }
    }
  }
}

// example
someAsyncStream
  .map { ... }
  .filter { ... }
  .eraseToAsyncThrowingStream() // use as a common return type

```

Would be nice if the APIs could be extended with something similar, but officially.

---

<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:** [November 11, 2022, 1:02pm UTC](https://forums.swift.org/t/missing-type-erasure/61377/2 "2022-11-11T13:02:32Z")

</div>

`AsyncSequence`s haven't adopted primary associated types yet since there is an ongoing discussion around the nature of typed throws. Exactly what you brought up basically because if `AsyncSequence`s adopt a primary associated type you want to be able to spell out the throwing effect as well.

Hypothetical code for this: `func foo() -> some/any throwing AsyncSequence<Int>`

In general, I agree that we really need to come to a decision on this topic though because being able to spell `some/any AsyncSequence` in whatever form is a fundamental tool for API design.

---

<div class="post-metadata">

**Author:** ![Srdan\_Rasic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/srdan_rasic/32/5396_2.png) [@Srdan\_Rasic](https://forums.swift.org/u/Srdan_Rasic)\
**Post date:** [November 22, 2022, 10:37pm UTC](https://forums.swift.org/t/missing-type-erasure/61377/3 "2022-11-22T22:37:43Z")

</div>

Where is that ongoing discussion happening? I know it was discussed when the primary associated types proposal was in review, but no decision was reached back then, and I haven’t seen any further progress there.

This problem is, in my opinion, the main blocker for a more widespread usage of the, otherwise amazing, async sequence algorithms library.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [November 23, 2022, 1:01am UTC](https://forums.swift.org/t/missing-type-erasure/61377/4 "2022-11-23T01:01:15Z")

</div>

> [@Srdan\_Rasic](#):
>
> This problem is, in my opinion, the main blocker for a more widespread usage of the, otherwise amazing, async sequence algorithms library.

How does your team manage the propagation of multiple error types along a series of calls?

---

<div class="post-metadata">

**Author:** ![Srdan\_Rasic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/srdan_rasic/32/5396_2.png) [@Srdan\_Rasic](https://forums.swift.org/u/Srdan_Rasic)\
**Post date:** [November 25, 2022, 10:58am UTC](https://forums.swift.org/t/missing-type-erasure/61377/5 "2022-11-25T10:58:26Z")

</div>

We generally don't expose typed errors in our interfaces, i.e. we use `Swift.Error`.

We did start by exposing typed errors across our codebase, but it turned out to be quite a burden with no obvious benefits.

Combining and mapping reactive streams, propagating errors from underlying services and so on led to having many different error types with `other` or `underlying` cases, or specific cases for all possible errors from other components. It was just not very ergonomic.

On top of that, at the point of use, we could almost never handle all possible error cases. Usually few cases would be handled in a specific way, while all other errors just got piped into a generic error UI.

Those two points made us stop exposing typed errors going forward.

That being said, it would be nice not to lose the error type, but the language would need to provide an ergonomic solution for combining and propagating multiple error types.

If a function needs to throw multiple error types, or if we need to combine multiple streams with different error types, there needs to be a built in solution that infers the combined error type, maybe as an anonymous sum type or something similar, for typed errors / throws to be ergonomic enough to work with, in my opinion.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 25, 2022, 12:52pm UTC](https://forums.swift.org/t/missing-type-erasure/61377/6 "2022-11-25T12:52:45Z")

</div>

The possible solution for that problem was already mention by John McCall. We will still only allow to throw a single error. You have to throw a common error type though. If you throw errors A and B and they have no relation to another then you'd throw Error. If you throw X and Y errors and both are subtypes of error S, then you throw S instead of error. If you throw C and D errors and D is a subtype of C, then you throw C.

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [November 25, 2022, 1:24pm UTC](https://forums.swift.org/t/missing-type-erasure/61377/7 "2022-11-25T13:24:41Z")

</div>

If that meant that:

```swift
protocol SomeService {
  var events: any throwing AsyncSequence<Event> { get }
}

```

Could be legally implemented by:

```swift
final class MyService: SomeService {
  enum MyServiceError: Error { ... }
  var events: any throwing(MyServiceError) AsyncSequence<Event> { ... }
}

```

I think that would be a fairly decent compromise. In practice I've found that typed throws (via Result/Publisher) tends to 'encoding the dependency tree'. However, I accept that they'd be useful in limited circumstances, particularly for local control flow.

In the end, I'll just be happy to see a decision reached so that the rough edges around asynchronous sequences can be finally ironed out.
