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

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, and wrote:

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:

/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?

  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.

@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)
  }
}

Not a compelling one, no.

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