示例工程 23-async-sequences-streams

23-async-sequences-streams/Diagnostics/MissingAwaitNext.swift

swift · 0.5 KB

// 故意编译失败:迭代器的 next() 是 async,必须 await。
// 复现:swiftc -typecheck Diagnostics/MissingAwaitNext.swift
func consume(_ sequence: CounterSequence) async {
    var iterator = sequence.makeAsyncIterator()
    let value = iterator.next()
    print(value as Any)
}
 
struct CounterSequence: AsyncSequence {
    typealias Element = Int
 
    struct AsyncIterator: AsyncIteratorProtocol {
        mutating func next() async -> Int? { nil }
    }
 
    func makeAsyncIterator() -> AsyncIterator { AsyncIterator() }
}