Share
Embed
Transcript
107 slides
-
ଟ͍ʙ • SE-0298 Async/Await: Sequences • SE-0314 AsyncStream and AsyncThrowingStream
• SE-0388 Convenience Async[Throwing]Stream.makeStream methods • SE-0406 Backpressure support for AsyncStream • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
͢͜ͱ • ͢͜ͱ • S_ShimotoriͷউखͳಡΈ৺ϨϏϡʔɹ˒˒˒ ⭐︎ ⭐︎ • S_Shimotori͕໘ന͍ͱࢥͬͨ •
͞ͳ͍͜ͱ • AsyncSequenceͱAsyncStreamͷ͍ํ -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences • SE-0314 AsyncStream and AsyncThrowingStream
• SE-0388 Convenience Async[Throwing]Stream.makeStream methods • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences • SE-0314 AsyncStream and AsyncThrowingStream
• SE-0388 Convenience Async[Throwing]Stream.makeStream methods • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
SE-0298 • https://github.com/swiftlang/swift-evolution/blob/main/proposals/0298- asyncsequence.md • Swift 5.5ʢ2021ʣ • iOS 13.0ͷόοΫϙʔτ͋Γ
• λΠτϧϑΝΠϧ໊ͷ௨ΓAsyncSequenceͷ https://developer.apple.com/documentation/Swift/AsyncSequence -
͜Εͷ͜ͱͰ͢ʂ • ͳΜ͔Α͘for awaitจͰ͙Δ͙Δճ͞ΕͯΔͭ for try await line in myFile.lines()
{ // Do something with each line } https://developer.apple.com/documentation/Swift/AsyncSequence ͜Ε -
SE-0296 Async/awaitʢલఏͷϓϩϙʔβϧʣ • ͍͔ͭকདྷͷ͋Δ࣌Ͱ୯ҰͷΛฦ͢ let dataResource = try await loadWebResource("dataprofile.txt")
https://github.com/swiftlang/swift-evolution/blob/main/proposals/0296-async-await.md -
SE-0298 Async/Await: Sequences • ࣌ؒͷܦաͱͱʹෳͷΛฦ͢ for try await line in
myFile.lines() { // Do something with each line } https://github.com/swiftlang/swift-evolution/blob/main/proposals/0298-asyncsequence.md -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences ✅ • SE-0314 AsyncStream and
AsyncThrowingStream • SE-0388 Convenience Async[Throwing]Stream.makeStream methods • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
SE-0314 • https://github.com/swiftlang/swift-evolution/blob/main/proposals/0314- async-stream.md • Swift 5.5ʢ2021ʣ • iOS 13.0ͷόοΫϙʔτ͋Γ
• λΠτϧϑΝΠϧ໊ͷ௨ΓAsyncStreamͷ https://developer.apple.com/documentation/Swift/AsyncStream -
init(_:bufferingPolicy:_:) class QuakeMonitor { var quakeHandler: ((Quake) -> Void)? func
startMonitoring() { ... } func stopMonitoring() { ... } static var quakes: AsyncStream<Quake> { AsyncStream { continuation in let monitor = QuakeMonitor() monitor.quakeHandler = { quake in continuation.yield(quake) } continuation.onTermination = { @Sendable _ in monitor.stopMonitoring() } monitor.startMonitoring() } } } https://developer.apple.com/documentation/Swift/AsyncStream ͜Ε -
ܧଓ……ʁ https://developer.apple.com/documentation/swift/withcheckedcontinuation(isolation:function:_:) func withCheckedContinuation<T>( isolation: isolated (any Actor)? = #isolation,
function: String = #function, _ body: (CheckedContinuation<T, Never>) -> Void ) async -> sending T -
Continuationͩʂ • withCheckedContinuation { continuation in } ͬͯͭ func withCheckedContinuation<T>(
_ body: (CheckedContinuation<T, Never>) -> Void ) async -> sending T https://developer.apple.com/documentation/swift/withcheckedcontinuation(isolation:function:_:) -
ܧଓ……ʁ class QuakeMonitor { var quakeHandler: ((Quake) -> Void)? func
startMonitoring() { ... } func stopMonitoring() { ... } static var quakes: AsyncStream<Quake> { AsyncStream { continuation in let monitor = QuakeMonitor() monitor.quakeHandler = { quake in continuation.yield(quake) } continuation.onTermination = { @Sendable _ in monitor.stopMonitoring() } monitor.startMonitoring() } } } https://developer.apple.com/documentation/Swift/AsyncSequence -
class QuakeMonitor { var quakeHandler: ((Quake) -> Void)? func startMonitoring()
{ ... } func stopMonitoring() { ... } static var quakes: AsyncStream<Quake> { AsyncStream { continuation in let monitor = QuakeMonitor() monitor.quakeHandler = { quake in continuation.yield(quake) } continuation.onTermination = { @Sendable _ in monitor.stopMonitoring() } monitor.startMonitoring() } } } AsyncStream { continuation in continuation.yield(quake) continuation.onTermination = { } } Continuationͩʂ https://developer.apple.com/documentation/Swift/AsyncSequence AsyncStream.Continuation -
AsyncStream.Continuation • ಉظίʔυͱඇಉظίʔυؒΛ݁ͼ͚ͭΔػߏɻ • init(_:bufferingPolicy:_:)ͰAsyncStreamʹͨ͠Ϋϩʔδϟɺ ݺͼग़͞ΕΔͱ͜ͷܕͷΠϯελϯεΛड͚औΔɻ͜ͷܧଓΛͬͯɺyield ϝιουͷ͍ͣΕ͔ΛݺͿ͜ͱͰετϦʔϜʹཁૉΛ͠ɺfinish()ϝιο υΛݺͿ͜ͱͰετϦʔϜΛਖ਼ৗʹऴྃ͢Δɻ • SwiftͷଞͷܧଓͱҟͳΓɺAsyncStream.ContinuationΤεέʔϓΛα
ϙʔτ͢Δɻ https://developer.apple.com/documentation/swift/asyncstream/continuation -
AsyncSequenceͱAsyncStreamͷཱͪҐஔ 1. SwiftʹඇಉظؔΛಋೖ͠ɺ୯ҰͷΛฦͤΔΑ͏ʹ͢ΔʢSE-0296ʣ 2. SwiftʹAsyncSequenceΛಋೖ͠ɺ࣌ؒܦաͰෳͷΛฦͤΔΑ͏ʹ͢ΔʢSE-0298ʣ 3. withCheckedContinuationʢSE-0300ʣ1͚ͩྃΛ௨͠ɺ ಉظؔͱඇಉظؔͷڮ͠Λ୲͏ • ͦΕΛ࣮ݱ͢ΔͨΊʹܧଓΛ͍ͬͯΔ
4. AsyncStreamʢSE-0314ʣෳճʹΘͨͬͯΛ௨͠ɺ ಉظؔͱඇಉظؔͷڮ͠Λ୲͏ • ͦΕΛ࣮ݱ͢ΔͨΊʹܧଓͱAsyncSequenceΛ͍ͬͯΔ -
AsyncSequenceͱAsyncStreamͷཱͪҐஔ 1. SwiftʹඇಉظؔΛಋೖ͠ɺ୯ҰͷΛฦͤΔΑ͏ʹ͢ΔʢSE-0296ʣ 2. SwiftʹAsyncSequenceΛಋೖ͠ɺ࣌ؒܦաͰෳͷΛฦͤΔΑ͏ʹ͢ΔʢSE-0298ʣ 3. withCheckedContinuationʢSE-0300ʣ1͚ͩྃΛ௨͠ɺ ಉظؔͱඇಉظؔͷڮ͠Λ୲͏ • ͦΕΛ࣮ݱ͢ΔͨΊʹܧଓΛ͍ͬͯΔ
4. AsyncStreamʢSE-0314ʣෳճʹΘͨͬͯΛ௨͠ɺ ಉظؔͱඇಉظؔͷڮ͠Λ୲͏ • ͦΕΛ࣮ݱ͢ΔͨΊʹܧଓͱAsyncSequenceΛ͍ͬͯΔ -
init(unfolding:onCancel:) • () async -> Element?Λड͚औͬͯޙΑ͠ͳʹͬͯ͘ΕΔͭ • ͜ͷΠχγϟϥΠβΫϩʔδϟΛ”ల։”ͯ͠ඇಉظετϦʔϜʹ͢Δɻ࡞ ͞ΕͨετϦʔϜɺऴྃॲཧʢΩϟϯηϧ͢Δ͔෮ॲཧͷऴྃͷͨΊʹ Ϋϩʔδϟ͔ΒnilΛฦ͢ʣؚΊͯAsyncSequenceϓϩτίϧࣗಈతʹ
४ڌ͢Δɻ https://developer.apple.com/documentation/swift/asyncstream/init(unfolding:oncancel:) -
init(unfolding:onCancel:) • () async -> Element?Λड͚औͬͯޙΑ͠ͳʹͬͯ͘ΕΔͭ • ͜ͷΠχγϟϥΠβΫϩʔδϟΛ”ల։”ͯ͠ඇಉظετϦʔϜʹ͢Δɻ࡞ ͞ΕͨετϦʔϜɺऴྃॲཧʢΩϟϯηϧ͢Δ͔෮ॲཧͷऴྃͷͨΊʹ Ϋϩʔδϟ͔ΒnilΛฦ͢ʣؚΊͯAsyncSequenceϓϩτίϧࣗಈతʹ
४ڌ͢Δɻ https://developer.apple.com/documentation/swift/asyncstream/init(unfolding:oncancel:) unfold -
Haskellͷunfolding • unfoldr :: (b -> Maybe (a, b)) ->
b -> [a] • SwiftͰॻ͘ͱ func unfoldr<T, U>(f: (U) -> (T, U)?, seed: U) -> [T] • SwiftͬΆ͘આ໌͢Δͱ ΫϩʔδϟγʔυseedΛड͚औΓɺArrayͷੜ͕͍ྃͯ͠Εnil Λฦ͢ɻͦ͏Ͱͳ͚Ε(t, u)Λฦ͠ɺtArrayͷઌ಄ʹՃ͞Εɺu࣍ ͷ࠶ؼݺͼग़͠Ͱ͏γʔυͱͳΔɻ • c.f. HaskellͷfoldrɺSwiftͷreduce(_:_:) https://hackage.haskell.org/package/base-4.21.0.0/docs/Data-List.html#v:unfoldr -
AsyncStreamΛ࡞Δํ๏ʢ࠶ܝʣ • init(_:bufferingPolicy:_:) 👈 ܧଓΛ͏ • init(unfolding:onCancel:) 👈 ΫϩʔδϟΛ”ల։”͢Δ •
makeStream(of:bufferingPolicy:) 👈 SE-0388Ͱޙ͔ΒՃ -
ײ • SE-0300ΛಡΜͩ΄͏͕ͨΊʹͳΓͦ͏ • ݁ߏڍಈ͕͍͜͠Β͍͠ɻɻ • ͜Μͳਓʹ͓͢͢Ί • Continuation͕͖ͳਓ •
AsyncStreamΛ͍͗ͯ͢ເʹग़͖ͯͨਓ https://zenn.dev/kntk/articles/0001fdd2a4f76d -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences ✅ • SE-0314 AsyncStream and
AsyncThrowingStream ✅ • SE-0388 Convenience Async[Throwing]Stream.makeStream methods • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
SE-0388 • https://github.com/swiftlang/swift-evolution/blob/main/proposals/0388- async-stream-factory.md • Swift 5.9ʢ2023ʣ • iOS 13.0ͷόοΫϙʔτ͋Γ
• AsyncStreamͷmakeStream(of:bufferingPolicy:)ͷ https://developer.apple.com/documentation/swift/asyncstream/makestream(of:bu ff eringpolicy:) -
͜Εͷ͜ͱͰ͢ʂ let (stream, continuation) = AsyncStream.makeStream(of: Int.self) await withTaskGroup(of: Void.self)
{ group in group.addTask { for i in 0...9 { continuation.yield(i) } continuation.finish() } group.addTask { for await i in stream { print(i) } } } ͜Ε -
class QuakeMonitor { var quakeHandler: ((Quake) -> Void)? func startMonitoring()
{ ... } func stopMonitoring() { ... } static var quakes: AsyncStream<Quake> { AsyncStream { continuation in let monitor = QuakeMonitor() monitor.quakeHandler = { quake in continuation.yield(quake) } continuation.onTermination = { @Sendable _ in monitor.stopMonitoring() } monitor.startMonitoring() } } } AsyncStream { continuation in continuation.yield(quake) continuation.onTermination = { } } Continuationͩʂʢ࠶ʣ https://developer.apple.com/documentation/Swift/AsyncSequence -
AsyncStream.Continuationʢ࠶ʣ • ಉظίʔυͱඇಉظίʔυؒΛ݁ͼ͚ͭΔػߏɻ • init(_:bufferingPolicy:_:)ͰAsyncStreamʹͨ͠Ϋϩʔδϟɺ ݺͼग़͞ΕΔͱ͜ͷܕͷΠϯελϯεΛड͚औΔɻ͜ͷܧଓΛͬͯɺyield ϝιουͷ͍ͣΕ͔ΛݺͿ͜ͱͰετϦʔϜʹཁૉΛ͠ɺfinish()ϝιο υΛݺͿ͜ͱͰετϦʔϜΛਖ਼ৗʹऴྃ͢Δɻ • SwiftͷଞͷܧଓͱҟͳΓɺAsyncStream.ContinuationΤεέʔϓΛα
ϙʔτ͢Δɻ https://developer.apple.com/documentation/swift/asyncstream/continuation -
࣮Ϋϩʔδϟͷ֎ʹऔΓग़ͤͪΌ͏ͷͰ͢ʂ • ݟͨʹ͢Δ • ͔͠ͳΜ͔μα͍ var continuation: AsyncStream<Int>.Continuation! AsyncStream<Int> {
continuation = $0 } continuation.finish() -
ετϦʔϜͱܧଓͷ྆ํΛฦ͢ʂ let (stream, continuation) = AsyncStream.makeStream(of: Int.self) await withTaskGroup(of: Void.self)
{ group in group.addTask { for i in 0...9 { continuation.yield(i) } continuation.finish() } group.addTask { for await i in stream { print(i) } } } -
ؾʹͳΔ… let (stream, continuation) = AsyncStream.makeStream(of: Int.self) await withTaskGroup(of: Void.self)
{ group in group.addTask { for i in 0...9 { continuation.yield(i) } continuation.finish() } group.addTask { for await i in stream { print(i) } } } Γ͕͘͠λϓϧ -
ͦΕͧΕߦ͖ઌ͕ҧ͏ʂ let (stream, continuation) = AsyncStream.makeStream(of: Int.self) await withTaskGroup(of: Void.self)
{ group in group.addTask { for i in 0...9 { continuation.yield(i) } continuation.finish() } group.addTask { for await i in stream { print(i) } } } ܧଓproducer͕͏ ετϦʔϜconsumer͕͏ -
Swift 5.5ʢ2021ʣͷࠒͷ • ΄΅ಉ࣌ʹiOS 15.0͕ग़ͨ • ଟΈΜͳ3όʔδϣϯͿΜʢiOS 13.0·Ͱʣ͘Β͍Λαϙʔτͯ͠Δ • جຊతʹ৽API͕ग़ͯաڈόʔδϣϯͷαϙʔτΛΊΔ·Ͱ͑ͳ͍
Swift Concurrency͕iOS 13.0Ͱ͑Εϋοϐʔʂʂʂʂʂ όοΫϙʔτ͞ΕΕ -
όοΫϙʔτ͞Εͨྫ • Swift Concurrency • viewIsAppearing(_:) • ͱ͔ • ৄ͘͠ڈͷiOSDC
Japanͷauramagi͞ΜͷൃදΛ͝ཡ͍ͩ͘͞ https://fortee.jp/iosdc-japan-2024/proposal/f30647c8-dd78-4853-8718-7bcb856b1239 https://www.docswell.com/s/usami-k/5M1P4L-uiviewcontroller-viewisappearing -
ද • 20219݄ Swift 5.5ͰSwift ConcurrencyσϏϡʔ • 202112݄ Xcode 13.2ͰSwift
Concurrency͕iOS 13.0όοΫϙʔτ͞ΕΔ • 20233݄ Swift 5.8Ͱ@backDeployed͕Ճ͞ΕΔ • 20239݄ Swift 5.9ͰmakeStream͕Ճ͞ΕΔɺόοΫσϓϩΠ͞ΕΔ https://speakerdeck.com/satotakeshi/swift-concurrencyru-men https://team-blog.mitene.us/%E5%AE%B6%E6%97%8F%E3%82%A2%E3%83%AB%E3%83%90%E3%83%A0-%E3%81%BF%E3%81%A6%E3%81%AD-%E3%81%ABswift- concurrency%E3%82%92%E5%B0%8E%E5%85%A5%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F-1bafc45ab3f3 -
ද • 20219݄ Swift 5.5ͰSwift ConcurrencyσϏϡʔ • 202112݄ Xcode 13.2ͰSwift
Concurrency͕iOS 13.0όοΫϙʔτ͞ΕΔ • 20233݄ Swift 5.8Ͱ@backDeployed͕Ճ͞ΕΔ • 20239݄ Swift 5.9ͰmakeStream͕Ճ͞ΕΔɺόοΫσϓϩΠ͞ΕΔ https://speakerdeck.com/satotakeshi/swift-concurrencyru-men https://team-blog.mitene.us/%E5%AE%B6%E6%97%8F%E3%82%A2%E3%83%AB%E3%83%90%E3%83%A0-%E3%81%BF%E3%81%A6%E3%81%AD-%E3%81%ABswift- concurrency%E3%82%92%E5%B0%8E%E5%85%A5%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F-1bafc45ab3f3 👇ʁʁʁʁʁ👆 -
ද • 20219݄ Swift 5.5ͰSwift ConcurrencyσϏϡʔ • 202112݄ Xcode 13.2ͰSwift
Concurrency͕iOS 13.0όοΫϙʔτ͞ΕΔ • 20233݄ Swift 5.8Ͱ@backDeployed͕Ճ͞ΕΔʢSE-0376ʣ • 20239݄ Swift 5.9ͰmakeStream͕Ճ͞ΕΔɺόοΫσϓϩΠ͞ΕΔ https://speakerdeck.com/satotakeshi/swift-concurrencyru-men https://team-blog.mitene.us/%E5%AE%B6%E6%97%8F%E3%82%A2%E3%83%AB%E3%83%90%E3%83%A0-%E3%81%BF%E3%81%A6%E3%81%AD-%E3%81%ABswift- concurrency%E3%82%92%E5%B0%8E%E5%85%A5%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F-1bafc45ab3f3 -
Ͱɺ@backDeployed͕Կͩͬͯʁ • چOSʹଘࡏ͠ͳ͍ؔΛίϐʔ͖ͯͯ͑͠ΔΑ͏ʹ͢ΔΈ • makeStreamͷΓΛ৽نͷܕʹ͢Δͱɺ@backDeployedΛཔΕͳ͘ͳΔ struct ৽͍͠ܕ { var continuation:
Continuation var stream: AsyncStream } @backDeployed(ུ) func makeStream() -> ৽͍͠ܕ // ແཧͦ͏ -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences ✅ • SE-0314 AsyncStream and
AsyncThrowingStream ✅ • SE-0388 Convenience Async[Throwing]Stream.makeStream methods ✅ • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
͜Ε͕૿͑·ͨ͠ʂ protocol AsyncIteratorProtocol<Element, Failure> { associatedtype Failure: Error = any
Error mutating func next( isolation actor: isolated (any Actor)? ) async throws(Failure) -> Element? } -
͜Εͷ͜ͱͰ͢ʂʢ࠶ʣ protocol AsyncIteratorProtocol<Element, Failure> { associatedtype Failure: Error = any
Error mutating func next( isolation actor: isolated (any Actor)? ) async throws(Failure) -> Element? } -
Primary associated typeͰͰ͖Δ͜ͱ • 👇͕࣮Ͱ͖Δ extension AsyncSequence { public func
map<Transformed>( _ transform: @Sendable @escaping (Element) async throws -> Transformed ) -> some AsyncSequence<Transformed, any Error> { ... } } ΊͬͪΌmap fi lterΛͯ͠ೖΕࢠʹͳΒͳ͍🎉 ※ඪ४ϥΠϒϥϦͷmap(_:)ͷΓͷܕ૬มΘΒͣAsyncThrowingMapSequence ࣗͰsome AsyncSequenceʹ͠Α͏ʂ -
AsyncIteratorProtocolͱthrow • next()ྫ֎Λ͛͏Δ͕ɺඞཁͳ͍ͷʹ·ͰtryΛॻ͘ͷͩΔ͍ • ίϯύΠϥ͕ඞཁͰͳ͍͜ͱΛݕ͢ΕɺলུΛೝΊΒΕΔ for try await value in
throwingAsyncSequence { } for await value in nonThrowingAsyncSequence { } ͍Δ ͍Βͳ͍ -
ίϯύΠϥ͕ݕ͢Δํ๏ • AsyncSequenceAsyncSequenceIteratorࣗମʹ@rethrows͕͍ͭͯͨ • աڈܗ @rethrows public protocol AsyncSequence {
@rethrows public protocol AsyncIteratorProtocol { https://github.com/swiftlang/swift/blob/9067148bc9c9a72730dbeebef32965b3aaeb34fb/stdlib/public/Concurrency/AsyncSequence.swift#L75-L76 https://github.com/swiftlang/swift/blob/9067148bc9c9a72730dbeebef32965b3aaeb34fb/stdlib/public/Concurrency/AsyncIteratorProtocol.swift#L90-L91 -
@rethrows protocol • Experimental featureʢͩͬͨΒ͍͠ʣ • some AsyncSequenceͱซ༻͍͕ͨͦ͠͏͢Δͱ@rethrows͕ػೳ͠ͳ͍ • ͳͷͰSwift
6.0͔Β@rethrowsΛ͏ͷΛΊͨͬΆ͍ • ৄ͘͠Point-Freeͷهࣄɺ͘͠ڈͷtreastrain͞Μͷൃද https://github.com/swiftlang/swift/commit/bb7a563e6c7756dc6c3c910db7ecacab2e1698b7 https://fortee.jp/iosdc-japan-2024/proposal/c0c62dc1-992c-4d9f-8812-02994593d96c https://www.pointfree.co/blog/posts/73-parser-errors -
SE-0421ͷ͓͠ΖτϐοΫ • AsyncSequenceͱprimary associated type ✅ • AsyncIteratorProtocolͱ@rethrows ✅ •
AsyncIteratorProtocolͱisolation -
ӽ͑Δͱ͖ͬͯͲΜͳͱ͖ @MainActor func iterate(over stream: AsyncStream<Int>) async { var iterator
= stream.makeAsyncIterator() while let element = await iterator.next() { // do something with 'element' } } https://github.com/swiftlang/swift-evolution/blob/main/proposals/0420-inheritance-of-actor-isolation.md -
ӽ͑Δͱ͖ͬͯͲΜͳͱ͖ @MainActor func iterate(over stream: AsyncStream<Int>) async { var iterator
= stream.makeAsyncIterator() while let element = await iterator.next() { // do something with 'element' } } warning: passing argument of non-sendable type 'inout AsyncStream<Int>.Iterator' outside of main actor-isolated context may introduce data races https://github.com/swiftlang/swift-evolution/blob/main/proposals/0420-inheritance-of-actor-isolation.md -
ӽ͑Δͱ͖ͬͯͲΜͳͱ͖ @MainActor func iterate(over stream: AsyncStream<Int>) async { var iterator
= stream.makeAsyncIterator() while let element = await iterator.next() { // do something with 'element' } } ؔࣗମMainActorʹִ͞ΕͯΔ ͍ͭ͜nonisolated https://github.com/swiftlang/swift-evolution/blob/main/proposals/0420-inheritance-of-actor-isolation.md -
ӽ͑Δͱ͖ͬͯͲΜͳͱ͖ @MainActor func iterate(over stream: AsyncStream<Int>) async { var iterator
= stream.makeAsyncIterator() while let element = await iterator.next() { // do something with 'element' } } next()ͷҾͱΓͰ͋Δ͜ͷ2ͭڥքΛӽ͑Δ͜ͱʹͳΔ https://github.com/swiftlang/swift-evolution/blob/main/proposals/0420-inheritance-of-actor-isolation.md -
AsyncIteratorProtocolͱisolation ʢ6.0Ҏ্ͷ࣌ʣ • #isolationΛड͚औΕΔΑ͏ͳnextϝιου͕͋Δ • func next(isolation actor: isolated (any
Actor)?) • SE-0414 Region based isolationͷ໊લ͕ग़͍ͯΔ͕ɺ SE-0414͕͜ͷΛղܾ͍ͯ͠ΔΘ͚Ͱͳ͍ɾɾͱࢥ͏ next(isolation: #isolation)Λۦ͢Δ͜ͱͰɺRegion based isolationΛՃຯͨ͠ݱࡏͷconcurrency checkΛύε͠Α͏ͶɺͱಡΊͳ͍͜ͱͳ͍ -
AsyncIteratorProtocolͱisolation ʢ6.0Ҏ্ͷ࣌ʣ • #isolationΛड͚औΕΔΑ͏ͳnextϝιου͕͋Δ • func next(isolation actor: isolated (any
Actor)?) • SE-0414 Region based isolationͷ໊લ͕ग़͍ͯΔ͕ɺ SE-0414͕͜ͷΛղܾ͍ͯ͠ΔΘ͚Ͱͳ͍ɾɾͱࢥ͏ next(isolation: #isolation)Λۦ͢Δ͜ͱͰɺRegion based isolationΛՃຯͨ͠ݱࡏͷconcurrency checkΛύε͠Α͏ͶɺͱಡΊͳ͍͜ͱͳ͍ SE-0313ʢSwift 5.5ʣ͔Β͋Δ -
AsyncIteratorProtocolͱisolation ʢ6.0Ҏ্ͷ࣌ʣ • #isolationΛड͚औΕΔΑ͏ͳnextϝιου͕͋Δ • func next(isolation actor: isolated (any
Actor)?) • SE-0414 Region based isolationͷ໊લ͕ग़͍ͯΔ͕ɺ SE-0414͕͜ͷΛղܾ͍ͯ͠ΔΘ͚Ͱͳ͍ɾɾͱࢥ͏ next(isolation: #isolation)Λۦ͢Δ͜ͱͰɺRegion based isolationΛՃຯͨ͠ݱࡏͷconcurrency checkΛύε͠Α͏ͶɺͱಡΊͳ͍͜ͱͳ͍ non-isolatedΛදݱ͢ΔͨΊʹඞཁ ݺͼग़͠ଆͷִ -
SE-0421ͷ͓͠ΖτϐοΫ • AsyncSequenceͱprimary associated type ✅ • AsyncIteratorProtocolͱ@rethrows ✅ •
AsyncIteratorProtocolͱisolation ✅ -
Ͱɺpolymorphism͕ͳΜͩͬͯʁ • Polymorphismͷجຊతͳҙຯɺʮ1 ͭͷؔ (͋Δ͍ϝιουɺࣜͳͲ) ͕ɺ༷ʑͳܕΛͭҾʹద༻Ͱ͖Δʯͱ͍͏ҙຯ by ஜେֶतۀ https://www.cs.tsukuba.ac.jp/~kam/lecture/plm2010/12.pdf protocol
AsyncIteratorProtocol<Element, Failure> { associatedtype Failure: Error = any Error mutating func next( isolation actor: isolated (any Actor)? ) async throws(Failure) -> Element? } -
ײ • Swift 6.0ɺΠϚυΩ͔ͬͯΜ͡ʙ • Swift 6Λཧղ͢Δඞཁʢଟʣ͋Δ͕͜Ε͡Όͳͯ͘SE-0420ΛಡΉ͖ • ͜Μͳਓʹ͓͢͢Ί •
ࣗͷSwift 6ͷཧղྗΛࢼ͍ͨ͠ਓ -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences ✅ • SE-0314 AsyncStream and
AsyncThrowingStream ✅ • SE-0388 Convenience Async[Throwing]Stream.makeStream methods ✅ • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol ✅ • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation -
αϯϓϧίʔυ var value: Int = 0 { didSet { for
c in continuations { c.yield(value) } } } var values: some AsyncSequence<Int, Never> { AsyncStream<Int>(bufferingPolicy: .bufferingNewest(1)) { continuation in continuation.yield(value) self.continuations.insert(continuation) continuation.onTermination = { _ in DispatchQueue.main.async { self.continuations.remove(continuation) } } } } private var continuations: Set<AsyncStream<Int>.Continuation> = [] -
αϯϓϧίʔυ var value: Int = 0 { didSet { for
c in continuations { c.yield(value) } } } var values: some AsyncSequence<Int, Never> { AsyncStream<Int>(bufferingPolicy: .bufferingNewest(1)) { continuation in continuation.yield(value) self.continuations.insert(continuation) continuation.onTermination = { _ in DispatchQueue.main.async { self.continuations.remove(continuation) } } } } private var continuations: Set<AsyncStream<Int>.Continuation> = [] where Element: Hashable -
ContinuationΛͲ͏ͬͯHashableʹ͢Δͷ • Continuationͷதclass _StorageͳͷͰ͍ͦͭΛ͏ • static func == (Self, Self)
-> Bool • _StorageͷಉҰੑΛݟΕOK • func hash(into: inout Hasher) • ObjectIdentifier(storage)ΛࠞͥΕOK https://github.com/swiftlang/swift/blob/8c1cc1b78a7bd4b46d53706e92ab864c25f09f04/stdlib/public/Concurrency/AsyncStream.swift#L478-L492 -
͍ͬͯͥ͘ʂ • SE-0298 Async/Await: Sequences ✅ • SE-0314 AsyncStream and
AsyncThrowingStream ✅ • SE-0388 Convenience Async[Throwing]Stream.makeStream methods ✅ • SE-0421 Generalize e ff ect polymorphism for AsyncSequence and AsyncIteratorProtocol ✅ • SE-0468 Hashable conformance for Async(Throwing)Stream.Continuation ✅