# Struggling to create a protocol that inherits from \`AsyncSequence\` with primary associated type

**URL:** <https://forums.swift.org/t/struggling-to-create-a-protocol-that-inherits-from-asyncsequence-with-primary-associated-type/73950>\
**Category:** Using Swift\
**Created:** [August 14, 2024, 2:59pm UTC](https://forums.swift.org/t/struggling-to-create-a-protocol-that-inherits-from-asyncsequence-with-primary-associated-type/73950 "2024-08-14T14:59:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lawrence-forooghian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lawrence-forooghian/32/27372_2.png) [@lawrence-forooghian](https://forums.swift.org/u/lawrence-forooghian)\
**Post date:** [August 14, 2024, 2:59pm UTC](https://forums.swift.org/t/struggling-to-create-a-protocol-that-inherits-from-asyncsequence-with-primary-associated-type/73950/1 "2024-08-14T14:59:08Z")

</div>

I wanted to create a protocol which represents an `AsyncSequence` with a primary associated type, so I followed the guidance given in, for example, [this post](https://forums.swift.org/t/primary-associated-types-for-asyncsequences/67099/2), and wrote:

```swift
struct Message {
    var text: String
}

protocol PrimaryAssociatedTypeAsyncSequence<Element>: AsyncSequence {
}

func use(_ sequence: any PrimaryAssociatedTypeAsyncSequence<Message>) async throws {
    for try await message in sequence {
        print(message.text)
    }
}

```

However, this gives the following compiler error:

```swift
/Users/lawrence/Desktop/async-sequence-protocols/Sources/async-sequence-protocols/async_sequence_protocols.swift:13:23: error: value of type 'Any' has no member 'text'
        print(message.text)
              ~~~~~~~^~~~
/Users/lawrence/Desktop/async-sequence-protocols/Sources/async-sequence-protocols/async_sequence_protocols.swift:13:23: note: cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members
        print(message.text)
                      ^
              ( as AnyObject)

```

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Quedlinbug](https://avatars.discourse-cdn.com/v4/letter/q/77aa72/32.png) [@Quedlinbug](https://forums.swift.org/u/Quedlinbug)\
**Post date:** [August 14, 2024, 5:32pm UTC](https://forums.swift.org/t/struggling-to-create-a-protocol-that-inherits-from-asyncsequence-with-primary-associated-type/73950/2 "2024-08-14T17:32:41Z")

</div>

1. Seems a bug. Changing `any` to `some` makes the error go away. I recommend doing in all cases anyway. Did you have some reason to restrict to existentials?

2. [`AsyncSequence` has primary associated types now.](https://developer.apple.com/documentation/swift/asyncsequence)

```swift
@available(macOS 15, iOS 18, *)
func use<Failure>(_ messages: some AsyncSequence<Message, Failure>) async throws(Failure) {
  for try await message in messages {
    print(message.text)
  }
}

```

---

<div class="post-metadata">

**Author:** ![lawrence-forooghian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lawrence-forooghian/32/27372_2.png) [@lawrence-forooghian](https://forums.swift.org/u/lawrence-forooghian)\
**Post date:** [August 14, 2024, 5:48pm UTC](https://forums.swift.org/t/struggling-to-create-a-protocol-that-inherits-from-asyncsequence-with-primary-associated-type/73950/3 "2024-08-14T17:48:34Z")

</div>

> [@Quedlinbug](#):
>
> Did you have some reason to restrict to existentials?

Not a compelling one, no.

> [@Quedlinbug](#):
>
> `AsyncSequence` has primary associated types now.

Yes, thanks! Unfortunately I am not yet targeting Swift 6.
