# Storing a \`.share()\`d AsyncSequence and conforming to a protocol

**URL:** <https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524>\
**Category:** Swift Async Algorithms\
**Created:** [February 2, 2026, 10:31pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524 "2026-02-02T22:31:06Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacecrafter3d](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spacecrafter3d/32/20929_2.png) [@spacecrafter3d](https://forums.swift.org/u/spacecrafter3d)\
**Post date:** [February 2, 2026, 10:31pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/1 "2026-02-02T22:31:07Z")

</div>

I’m creating a model object that should expose a shared stream of status updates:

```swift
actor MyClient: Client {
    let statusUpdates: any AsyncSequence<Status, Never> & Sendable

    init() {
        let (stream, continuation) = AsyncStream<Status>.makeStream()
        statusUpdates = stream.share()
    }
}

```

But that means my protocol must require `any AsyncSequence` instead of using an `associatedtype`. It feels wrong to encode the implementation details in the protocol like this. But I can't figure out another way to store the result of `share()`, because it returns an opaque type.

I can use a `lazy var`:

```swift
lazy var statusUpdates: some AsyncSequence<Status, Never> & Sendable = _statusUpdates.stream.share()
private let _statusUpdates = AsyncStream<Status>.makeStream()

```

But on my actor, that means I have to `await` access to the property, even though it should not be mutated after initialization.

I could create my own `AnyAsyncIterator` type eraser that does conform to the protocol, but that feels like a less-than-optimal solution.

What's the best way to solve this?

---

<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:** [February 2, 2026, 10:53pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/2 "2026-02-02T22:53:59Z")

</div>

> [@spacecrafter3d](#):
>
> I’m creating a model object that should expose a shared stream of status updates:
> 
> ```swift
> actor MyClient: Client {
> let statusUpdates: any AsyncSequence<Status, Never> & Sendable
> 
> init() {
> let (stream, continuation) = AsyncStream<Status>.makeStream()
> statusUpdates = stream.share()
> }
> }
> 
> ```

Who controls the `Client` protocol? Was that written by you? ~~What about the `share` method? Where did that come from?~~

The `share` is from here?

> <https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0016-share.md>

---

<div class="post-metadata">

**Author:** ![spacecrafter3d](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spacecrafter3d/32/20929_2.png) [@spacecrafter3d](https://forums.swift.org/u/spacecrafter3d)\
**Post date:** [February 2, 2026, 11:01pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/3 "2026-02-02T23:01:57Z")

</div>

Yes, I wrote `Client`:

It looks like:

```swift
protocol Client {
    associatedtype StatusUpdates: AsyncSequence<Status, Never> & Sendable
    var statusUpdates: StatusUpdates { get }
}

```

Yes it is the `.share()` included in `swift-async-algorithms`.

---

<div class="post-metadata">

**Author:** ![KeithBauerANZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keithbaueranz/32/27897_2.png) [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Post date:** [February 3, 2026, 2:09am UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/4 "2026-02-03T02:09:59Z")

</div>

Basically, you'll quickly run into "error: property definition has inferred type , involving the 'some' return type of another declaration".

Swift doesn't let you do this for, I believe, ABI reasons — changing the concrete implementation of a `some` type is allowed, so nothing that would result in encoding the name of the _current_ type into any other type, is allowed?

I don't see a way around this without an indirection — you could use a base class with a generic subclass, for example, or as you say, `any AsyncSequence` is fine. But I think any _direct_ use of the result of `.share()` will fall afoul of the above error.

If swift-async-algorithms declared `AsyncShareSequence` as public, and removed the `some` return type from `share()` in favor of declaring the concrete type, the problem would go away. I don't see why they _shouldn't_ do that; it doesn't impose much of a constraint on their long-term ABI evolution? But I don't know why they've opted for the current approach.

> [@spacecrafter3d](#):
>
> I can use a `lazy var`:

My gut is that this is a bug, since pretty much any other attempt to create a stored property of this type will result in an error…

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [February 3, 2026, 8:52am UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/5 "2026-02-03T08:52:18Z")

</div>

Your original snippet would compile if you allowed your `Client` protocol to change to:

```swift
protocol Client {
    var statusUpdates: any AsyncSequence<Status, Never> & Sendable { get }
}

```

Do you think there's enough of a performance reason not to do that?

* * *

That said, I also occasionally hit the wall with the ergonomics of opaque result types, precisely when there's need to carry the result around in a stored property. The language designers are clearly aware of the issue, and you can see it worked around via underscored attributes in the `swiftinterface` files for SwiftUI. Slava has even written some elaborate docs for it in his excellent documentation on [Compiling Swift Generics](https://download.swift.org/docs/assets/generics.pdf); see esp. chapter 13.2, pp. 457–459.

**Disclaimer: The following is not officially supported in Swift**

The way to make it work is by applying the `@_opaqueReturnTypeOf(mangling, index)` attribute on a placeholder name such as `__` (followed up with any generic arguments in angle brackets). I couldn't find a way to find the correct mangling for the opaque return type of the expression `stream.share()`, but if you move the whole setup into a function of your own, it all appears to work:

```swift
public struct Status: Sendable {}

protocol Client {
    associatedtype StatusUpdates: AsyncSequence<Status, Never> & Sendable
    var statusUpdates: StatusUpdates { get }
}

actor MyClient: Client {
    typealias StatusUpdates = @_opaqueReturnTypeOf(
        "$s8MyModule8MyClientC18shareStatusUpdatesQr_ScS12ContinuationVyAA0F0V_GtyFZ",
        0
    ) __

    private let continuation: AsyncStream<Status>.Continuation
    let statusUpdates: StatusUpdates

    init() {
        (statusUpdates, continuation) = Self.shareStatusUpdates()
    }

    static func shareStatusUpdates() -> (
        some AsyncSequence<Status, Never> & Sendable,
        AsyncStream<Status>.Continuation
    ) {
        let (stream, continuation) = AsyncStream<Status>.makeStream()
        return (stream.share(), continuation)
    }
}

```

The above assumes a module named `MyModule`. Apply `nm` command to your compiled library or executable to find your precise mangled name, and `swift demangle --help` for the opposite.

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [February 3, 2026, 9:03am UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/6 "2026-02-03T09:03:53Z")

</div>

> [@KeithBauerANZ](#):
>
> > [@spacecrafter3d](#):
> >
> > I can use a `lazy var`:
> 
> My gut is that this is a bug, since pretty much any other attempt to create a stored property of this type will result in an error…

No, I don't think this to be a bug at all. For a declaration with initial value, which `lazy var` always is, the return type is always visible to the compiler, so no trouble inferring the opaque type. And `lazy` is needed here because the initial value expression needs to refer the other stored property `stream`.

---

<div class="post-metadata">

**Author:** ![spacecrafter3d](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spacecrafter3d/32/20929_2.png) [@spacecrafter3d](https://forums.swift.org/u/spacecrafter3d)\
**Post date:** [February 3, 2026, 10:52am UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/7 "2026-02-03T10:52:07Z")

</div>

Woah! 😳

Thanks for the tips! The reason I posted is because storing the result of `.share()` in a property seems like the primary way it would be used, enabling it to be accessed from multiple contexts. I imagined the library authors must have an intended approach for this.

It seems like my options are `any` in the protocol, a `lazy var`, or a lot of boilerplate (a type-erasing wrapper or @pyrtsa's amazing solution). Avoiding boilerplate was why I wanted to use `.share()` in the first place, to replace my custom multicast solution of storing `Continuation`s in a `Dictionary` indexed by `UUID`s.

For my use case, I decided I don't really need multicast after all. My `Client` has a single `@Observed` owner with a `Task` that stores the status in a property.

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [February 3, 2026, 11:00am UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/8 "2026-02-03T11:00:39Z")

</div>

> [@spacecrafter3d](#):
>
> The reason I posted is because storing the result of `.share()` in a property seems like the primary way it would be used, enabling it to be accessed from multiple contexts. I imagined the library authors must have an intended approach for this.

I think this is important feedback on the current design of `share()`. I, too, don't understand why the return type had to be hidden like that.

Could @Philippe_Hausler shed some light on the current choice? Why not make the return type concrete (`... -> AsyncShareSequence<Base>`) and `public`?

---

<div class="post-metadata">

**Author:** ![spacecrafter3d](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spacecrafter3d/32/20929_2.png) [@spacecrafter3d](https://forums.swift.org/u/spacecrafter3d)\
**Post date:** [February 3, 2026, 11:07am UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/9 "2026-02-03T11:07:28Z")

</div>

Should I file an issue on GitHub, or is this the right place for the feedback?

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [February 3, 2026, 4:33pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/10 "2026-02-03T16:33:11Z")

</div>

The underlying issue is storing to the any rather than a generic. Opaque `any` types dont conform to the protocols they represent.

---

<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:** [February 3, 2026, 6:06pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/11 "2026-02-03T18:06:41Z")

</div>

> [@pyrtsa](#):
>
> I think this is important feedback on the current design of `share()`. I, too, don't understand why the return type had to be hidden like that.
> 
> Could @Philippe_Hausler shed some light on the current choice? Why not make the return type concrete (`... -> AsyncShareSequence<Base>`) and `public`?

I had a similar question on another pitch:

> [@\[AsyncAlgorithms\] FlatMapLatest algorithm](https://forums.swift.org/t/asyncalgorithms-flatmaplatest-algorithm/84404/2):
>
> I'm wondering why we should prefer an opaque some return value here on these. From what I can tell so far Standard Library APIs on AsyncSequence like map and compactMap return public concrete types: AsyncMapSequence and AsyncCompactMapSequence. And AsyncAlgorithms APIs on AsyncSequence like zip and combineLatest also return public types: AsyncZip2Sequence and AsyncCombineLatest3Sequence. I'm a fan of opaque return values and don't mean to block that… but I'm wondering if there was a little mor…

It sounds like in the era before opaque values and primary associated types the choice to return concrete values was out of necessity. But now that primary associated types are available it sounds like the general preference is to prefer opaque.

---

<div class="post-metadata">

**Author:** ![KeithBauerANZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keithbaueranz/32/27897_2.png) [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Post date:** [February 3, 2026, 9:03pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/12 "2026-02-03T21:03:58Z")

</div>

> [@pyrtsa](#):
>
> No, I don't think this to be a bug at all. For a declaration with initial value, which `lazy var` always is, the return type is always visible to the compiler, so no trouble inferring the opaque type. And `lazy` is needed here because the initial value expression needs to refer the other stored property `stream`.

I don't think it's a bug for either of these reasons; I think it's a bug because the compiler goes out of its way (with explicit errors) to prevent the creation of any property or method with a type dependent on another `some` type.

So I guess the question is "is this disallowed because the problem is with _writing_ the name, or is it disallowed because the problem is with _using the type in this position_?"

If the former, we should make an SE- to get a user-friendly `typeof` operator that we can use to spell opaque types, and the OP's problem will be solved by writing `typeof<AsyncStream<Status>.share()>` or similar.

If the latter, then `typeof` isn't a reasonable extension to the language, and `lazy var` working is certainly a bug.

---

<div class="post-metadata">

**Author:** ![KeithBauerANZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keithbaueranz/32/27897_2.png) [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Post date:** [February 3, 2026, 9:06pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/13 "2026-02-03T21:06:29Z")

</div>

> [@vanvoorden](#):
>
> It sounds like in the era before opaque values and primary associated types the choice to return concrete values was out of necessity. But now that primary associated types are available it sounds like the general preference is to prefer opaque.

I understand that people seem to have a general preference toward opaque types, and I can guess why (more flexibility to change the implementation later without affecting ABI). But given the significant downsides (as evidenced by this thread), and the relatively small cost of making the actual return type public (just be careful that each unique `some` gets a unique concrete type, if you want to preserve your ability to evolve the library ABI-compatibly), I'm not sure this is a _reasonable_ decision. Particularly not for swift-async-algorithms, which doesn't have any ABI concerns.

---

<div class="post-metadata">

**Author:** ![JacobHearst](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jacobhearst/32/31923_2.png) [@JacobHearst](https://forums.swift.org/u/JacobHearst)\
**Post date:** [February 5, 2026, 1:41pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/14 "2026-02-05T13:41:28Z")

</div>

My brain may still be turning on for the morning but is the `Client` protocol not already generic? It has an associated `StatusUpdates` type. Or are you saying that the `MyClient` type itself should be generic?

---

<div class="post-metadata">

**Author:** ![kelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kelin/32/21678_2.png) [@kelin](https://forums.swift.org/u/kelin)\
**Post date:** [June 12, 2026, 2:18pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/15 "2026-06-12T14:18:26Z")

</div>

This topic is especially complicated since `AsyncShareSequence` is internal. Otherwise I'd prefer to simply specify the full type, like `AsyncShareSequence<AsyncStream<Double, Never>>`. But we are limited to opaque and existential types...

---

<div class="post-metadata">

**Author:** ![kelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kelin/32/21678_2.png) [@kelin](https://forums.swift.org/u/kelin)\
**Post date:** [June 12, 2026, 4:16pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/16 "2026-06-12T16:16:01Z")

</div>

I created an issue: [Could we make AsyncShareSequence public? · Issue #431 · apple/swift-async-algorithms · GitHub](https://github.com/apple/swift-async-algorithms/issues/431)

There's also a PR: [Make AsyncShareSequence Type Public by AGiersch1804 · Pull Request #412 · apple/swift-async-algorithms · GitHub](https://github.com/apple/swift-async-algorithms/pull/412)

---

<div class="post-metadata">

**Author:** ![KeithBauerANZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keithbaueranz/32/27897_2.png) [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Post date:** [June 14, 2026, 9:07pm UTC](https://forums.swift.org/t/storing-a-share-d-asyncsequence-and-conforming-to-a-protocol/84524/17 "2026-06-14T21:07:06Z")

</div>

I agree, `some P` as a return type in public interfaces sounds reasonable on paper, but the restrictions on actually using that type are too egregious. This shows up in SwiftUI, in swift-async-algorithms, and even in the stdlib these days. Public API needs to use concrete types.
