Convenience Async[Throwing]Stream.makeStream methods
About 2 min
Convenience Async[Throwing]Stream.makeStream methods 관련
HACKING WITH SWIFT
What's new in Swift?
Convenience Async[Throwing]Stream.makeStream methods | Changes in Swift 5.9
Convenience Async[Throwing]Stream.makeStream methods
Available from Swift 5.9
SE-0388 (apple/swift-evolution
) adds a new makeStream()
method to both AsyncStream
and AsyncThrowingStream
that sends back both the stream itself alongside its continuation.
So, rather than writing code like this:
var _continuation: AsyncStream<String>.Continuation!
let stream = AsyncStream<String> { _continuation = $0 }
let continuation = _continuation!
We can now get both at the same time:
let (newStream, newContinuation) = AsyncStream.makeStream(of: String.self)
This is going to be particularly welcome in places where you need to access the continuation outside of the current context, such as in a different method. For example, previously we might have written a simple number generator like this one, which needs to store the continuation as its own property in order to be able to call it from the queueWork()
method:
struct OldNumberGenerator {
private var continuation: AsyncStream<Int>.Continuation!
var stream: AsyncStream<Int>!
init() {
stream = AsyncStream(Int.self) { continuation in
self.continuation = continuation
}
}
func queueWork() {
Task {
for i in 1...10 {
try await Task.sleep(for: .seconds(1))
continuation.yield(i)
}
continuation.finish()
}
}
}
With the new makeStream(of:)
method this code becomes much simpler:
struct NewNumberGenerator {
let (stream, continuation) = AsyncStream.makeStream(of: Int.self)
func queueWork() {
Task {
for i in 1...10 {
try await Task.sleep(for: .seconds(1))
continuation.yield(i)
}
continuation.finish()
}
}
}
Other Changes in Swift 5.9
if and switch expressions | Changes in Swift 5.9
if and switch expressions
Value and Type Parameter Packs | Changes in Swift 5.9
Value and Type Parameter Packs
Macros | Changes in Swift 5.9
Macros
Noncopyable structs and enums | Changes in Swift 5.9
Noncopyable structs and enums
consume operator to end the lifetime of a variable binding | Changes in Swift 5.9
consume operator to end the lifetime of a variable binding
Add sleep(for:) to Clock | Changes in Swift 5.9
Add sleep(for:) to Clock
Discarding task groups | Changes in Swift 5.9
Discarding task groups