AsyncSequence .first property

As mentioned in the original proposal for AsyncSequence, it was planned to add a .first property to consume a single element out of the sequence. Since the language requirement is now satisfied, and Swift 6 is around the corner, should we add it?

extension AsyncSequence {
    var first: Element? {
        get async throws { try await self.first(where: { _ in true }) }
    }
}
9 Likes

It should adopt typed throws (since SE-0421 is also accepted):

extension AsyncSequence {
    public var first: Element? {
        get async throws(Failure) {
            for try await value in self {
                return value
            }
            return nil
        }
    }
}
5 Likes