w ͜ͷϓϩϙʔβϧͰίϯϓϦʔγϣϯϋϯυϥͷଟ͘ͷΛഉআ w ࠷ҰൠతͳϢʔεέʔεʹ໋໊͔ͬͯ๏ͱ༻ޠΛόΠΞε͢Δ w BTZOD͔ZJFMET͔ʹ͍ͭͯίΞηϚϯςΟΫεʹؔ͠ͳ͍ w தஅʹؔ͢Δ"1*ʢCFHJO"TZOD TVTQFOE"TZODʣ w 'VUVSFʹΑͬͯฒྻ࣮ߦΛߦ͏݅ w 0CK$ͷίϯϓϦʔγϣϯϋϯυϥϕʔεͷ"1*Λ 4XJGUͰ͑ΔΑ͏ ม͢Δ w BTZODؔUISPXTͰ͖ΔΑ͏ʹ͠ɺUSZBXBJUͰϋϯυϦϯάͰ͖ΔΑ͏ʹ͢Δ w ίϯύΠϥΫϩʔδϟ͕ඇಉظ͔Ͳ͏͔؆୯ʹఆͰ͖Δ w EFGFSίϧʔνϯ͕ഁغ͞Εͨࡍʹಈ࡞͢ΔΑ͏ʹ͢Δ w ʢϋϯυϥͳΒҾΛ૿͕͢ʣBTZODͳSFUVSO݁ՌΛλϓϧͰฦͤΔ w ίϯύΠϥ͕ANZ2VFVFBTZOD\^Aͳهड़Λݕग़ͯ͠pYJUͰमਖ਼Ͱ͖Δ w "#*4UBCJMJUZʹӨڹ͠ͳ͍ w BTZODͳΒUISPXTͱ͢ΔͱύλʔϯݮΔ͕ܽ͋Δ w BTZODͳΒσϑΥϧτUISPXTʹ͠ɺBTZOD OPOUISPXJOH ʹ͢Δͷܽ͋Δ w ίϧʔνϯΛ༻ҙ͍ͨ͠ͷͰ͋ͬͯBTZODBXBJUΛ࣮͍ͨ͠ͷͰͳ͍ w $ͷΑ͏ʹҰൠతͳBTZODBXBJU'VUVSFΫϥεΛฦ͢͜ͱʹͳΔ w ͦ͏ͳΔͱBTZODBXBJUΛݴޠϨϕϧͰ࣮͢Δ͜ͱʹͳͬͯ͠·͏ w ͜ͷఏҊAYBXBJUGPP CBS AͷΑ͏ʹνΣΠχϯάΛ͏·͘Ͱ͖Δ w ۩ମྫͳ͍͕͜ͷఏҊ͔Βকདྷͷ֦ுੑɺ͍͖ͬͯΛड़͍ͯΔ ͜ͷϓϩϙʔβϧʹ͍ͭͯ ಈػίϯϓϦʔγϣϯϋϯυϥ࠷దͰͳ͍ ղܾࡦίϧʔνϯ 0CK$Πϯϙʔλͷม طଘػೳͱͷ૬ޓ࡞༻ աڈͷίʔυͱͷޓੑ "#*TUBCJMJUZͷӨڹ ଞͷγϯλοΫε ସҊ কདྷతͳํੑ ँࣙ
w ͜ͷϓϩϙʔβϧͰίϯϓϦʔγϣϯϋϯυϥͷଟ͘ͷΛഉআ w ࠷ҰൠతͳϢʔεέʔεʹ໋໊͔ͬͯ๏ͱ༻ޠΛόΠΞε͢Δ w BTZOD͔ZJFMET͔ʹ͍ͭͯίΞηϚϯςΟΫεʹؔ͠ͳ͍ w தஅʹؔ͢Δ"1*ʢCFHJO"TZOD TVTQFOE"TZODʣ w 'VUVSFʹΑͬͯฒྻ࣮ߦΛߦ͏݅ w 0CK$ͷίϯϓϦʔγϣϯϋϯυϥϕʔεͷ"1*Λ 4XJGUͰ͑ΔΑ͏ ม͢Δ w BTZODؔUISPXTͰ͖ΔΑ͏ʹ͠ɺUSZBXBJUͰϋϯυϦϯάͰ͖ΔΑ͏ʹ͢Δ w ίϯύΠϥΫϩʔδϟ͕ඇಉظ͔Ͳ͏͔؆୯ʹఆͰ͖Δ w EFGFSίϧʔνϯ͕ഁغ͞Εͨࡍʹಈ࡞͢ΔΑ͏ʹ͢Δ w ʢϋϯυϥͳΒҾΛ૿͕͢ʣBTZODͳSFUVSO݁ՌΛλϓϧͰฦͤΔ w ίϯύΠϥ͕ANZ2VFVFBTZOD\^Aͳهड़Λݕग़ͯ͠pYJUͰमਖ਼Ͱ͖Δ w "#*4UBCJMJUZʹӨڹ͠ͳ͍ w BTZODͳΒUISPXTͱ͢ΔͱύλʔϯݮΔ͕ܽ͋Δ w BTZODͳΒσϑΥϧτUISPXTʹ͠ɺBTZOD OPOUISPXJOH ʹ͢Δͷܽ͋Δ w ίϧʔνϯΛ༻ҙ͍ͨ͠ͷͰ͋ͬͯBTZODBXBJUΛ࣮͍ͨ͠ͷͰͳ͍ w $ͷΑ͏ʹҰൠతͳBTZODBXBJU'VUVSFΫϥεΛฦ͢͜ͱʹͳΔ w ͦ͏ͳΔͱBTZODBXBJUΛݴޠϨϕϧͰ࣮͢Δ͜ͱʹͳͬͯ͠·͏ w ͜ͷఏҊAYBXBJUGPP CBS AͷΑ͏ʹνΣΠχϯάΛ͏·͘Ͱ͖Δ w ۩ମྫͳ͍͕͜ͷఏҊ͔Βকདྷͷ֦ுੑɺ͍͖ͬͯΛड़͍ͯΔ ͜ͷϓϩϙʔβϧʹ͍ͭͯ ಈػίϯϓϦʔγϣϯϋϯυϥ࠷దͰͳ͍ ղܾࡦίϧʔνϯ 0CK$Πϯϙʔλͷม طଘػೳͱͷ૬ޓ࡞༻ աڈͷίʔυͱͷޓੑ "#*TUBCJMJUZͷӨڹ ଞͷγϯλοΫε ସҊ কདྷతͳํੑ ँࣙ
} private var result: Result? = nil private var awaiters: [(Result) -> Void] = [] // Fulfill the future, and resume any coroutines waiting for the value. func fulfill(_ value: T) { precondition(self.result == nil, "can only be fulfilled once") let result = .value(value) self.result = result for awaiter in awaiters { awaiter(result) } awaiters = [] } // Mark the future as having failed to produce a result. func fail(_ error: Error) { precondition(self.result == nil, "can only be fulfilled once") let result = .error(error) self.result = result for awaiter in awaiters { awaiter(result) } awaiters = [] } func get() async throws -> T { switch result { // Throw/return the result immediately if available. case .error(let e)?: throw e case .value(let v)?: return v // Wait for the future if no result has been fulfilled. case nil: return await suspendAsync { continuation, error in awaiters.append({ switch $0 { case .error(let e): error(e) case .value(let v): continuation(v) } }) } } } // Create an unfulfilled future. init() {} convenience init(_ body: () async -> T) { self.init() beginAsync { do { self.fulfill(await body()) } catch { self.fail(error) } } } } class Future<T> w ϓϩϙʔβϧʹΫϦε͞Μͷ࣮ྫ w ଞͷݴޠͰ1SPNJTF5BTL%FGFSSFE͕͋Δ
} private var result: Result? = nil private var awaiters: [(Result) -> Void] = [] // Fulfill the future, and resume any coroutines waiting for the value. func fulfill(_ value: T) { precondition(self.result == nil, "can only be fulfilled once") let result = .value(value) self.result = result for awaiter in awaiters { awaiter(result) } awaiters = [] } // Mark the future as having failed to produce a result. func fail(_ error: Error) { precondition(self.result == nil, "can only be fulfilled once") let result = .error(error) self.result = result for awaiter in awaiters { awaiter(result) } awaiters = [] } func get() async throws -> T { switch result { // Throw/return the result immediately if available. case .error(let e)?: throw e case .value(let v)?: return v // Wait for the future if no result has been fulfilled. case nil: return await suspendAsync { continuation, error in awaiters.append({ switch $0 { case .error(let e): error(e) case .value(let v): continuation(v) } }) } } } // Create an unfulfilled future. init() {} convenience init(_ body: () async -> T) { self.init() beginAsync { do { self.fulfill(await body()) } catch { self.fail(error) } } } } class Future<T> w ϓϩϙʔβϧʹΫϦε͞Μͷ࣮ྫ w ଞͷݴޠͰ1SPNJTF5BTL%FGFSSFE͕͋Δ HFUϝιουͰ TVTQFOE"TZODΛ͍ͬͯΔͷͰ͜Ε ΛཧղͰ͖Ε͞ΒʹBTZODBXBJUΛ શʹཧղͰ͖ͦ͏
} private var awaiters: [(Result) -> Void] = [] // লུ … func get() async throws -> T { switch result { // Throw/return the result immediately if available. case .error(let e)?: throw e case .value(let v)?: return v // Wait for the future if no result has been fulfilled. case nil: return await suspendAsync { continuation, error in awaiters.append({ switch $0 { case .error(let e): error(e) case .value(let v): continuation(v) } }) } } } } Futureͷgetʁ
} private var awaiters: [(Result) -> Void] = [] // লུ … func get() async throws -> T { switch result { // Throw/return the result immediately if available. case .error(let e)?: throw e case .value(let v)?: return v // Wait for the future if no result has been fulfilled. case nil: return await suspendAsync { continuation, error in awaiters.append({ switch $0 { case .error(let e): error(e) case .value(let v): continuation(v) } }) } } } } Futureͷgetʁ HFUΑΓલʹॲཧྃ͠GVMpMM͞Εͯ ΕHFU͞ΕͨΒ͙݁͢ՌΛฦ͢
} private var awaiters: [(Result) -> Void] = [] // লུ … func get() async throws -> T { switch result { // Throw/return the result immediately if available. case .error(let e)?: throw e case .value(let v)?: return v // Wait for the future if no result has been fulfilled. case nil: return await suspendAsync { continuation, error in awaiters.append({ switch $0 { case .error(let e): error(e) case .value(let v): continuation(v) } }) } } } } HFUΑΓલʹGVMpMM͞Εͯͳ͚Ε BXBJUFSΫϩʔδϟྻʹ DPOUJOVBUJPOΛ ݺͼग़͢ΫϩʔδϟΛBQQFOE͠ GVMpMM͞ΕͨλΠϛϯάͰ݁ՌΛฦͤΔ Futureͷgetʁ HFUΑΓલʹॲཧྃ͠GVMpMM͞Εͯ ΕHFU͞ΕͨΒ͙݁͢ՌΛฦ͢