Upgrade to Pro — share decks privately, control downloads, hide ads and more …

継続渡しと契約による設計 / 20190319 #tryswift_pre

継続渡しと契約による設計 / 20190319 #tryswift_pre

【増枠/increase】try! Swift Pre Talks 2019 - connpass
https://tryswifttokyo-aftertalks.connpass.com/event/120987/
での発表資料です。

takasek

March 19, 2019
Tweet

More Decks by takasek

Other Decks in Programming

Transcript

  1. // Without CPS func doSomething(i: Int) -> String { sleep(100)

    return "\(i)" } // With CPS func doSomething(i: Int, completion: @escaping (String) -> Void) { DispatchQueue.global().async { sleep(100) completion("\(i)") } } 5
  2. ! 6

  3. func doSomething( i: Int, completion: @escaping (String) -> Void )

    ͜ͷΠϯλϑΣʔεʹ͸ɺخ͘͠ͳ͍఺͕͋Γ·͢ 7
  4. ίʔυͱද໌ͷ۩ମྫ func half(of i: Int) throws -> Int { assert(i.isMultiple(of:

    2)) if !i.isMultiple(of: 2) { throw Error.isOdd } return i / 2 } • ࣄલ৚݅: • Int ͔͠ड͚͚ͭ·ͤΜ • ۮ਺͔͠ड͚͚ͭ·ͤΜ (ܕͱͯ͠දݱෆೳ) • ࣄޙ৚݅: ඞͣ Int Λฦ͠·͢ • ྫ֎: ۮ਺Ͱͳ͔ͬͨ৔߹͸ྫ֎Λ౤͛·͢ 14
  5. func half(of i: Int) throws -> Int { if !i.isMultiple(of:

    2) { throw Error.isOdd } return i / 2 } ͜ΕΛCPSʹม׵ͯ͠Έ·͠ΐ͏ 17
  6. func half(of i: Int, completion: (Int) -> Void) { if

    !i.isMultiple(of: 2) { return } completion(i / 2) } 18
  7. 19

  8. 21

  9. 23

  10. func half(of i: Int, completion: (Int) -> Void) { if

    !i.isMultiple(of: 2) { return // ! } completion(i / 2) } 25
  11. enum Result<T> { case success(T) case error(Error) } func half(of

    i: Int, completion: (Result<Int>) -> Void) { if !i.isMultiple(of: 2) { return // ! } completion(.success(i / 2)) } 26
  12. enum Result<T> { case success(T) case error(Error) } func half(of

    i: Int, completion: (Result<Int>) -> Void) { if !i.isMultiple(of: 2) { completion(.error(.isOdd)) // } completion(.success(i / 2)) } 27
  13. 29

  14. func half(of i: Int, completion: (Result<Int>) -> Void) { if

    !i.isMultiple(of: 2) { completion(.error(.isOdd)) // ! } completion(.success(i / 2)) } 31
  15. func half(of i: Int, completion: (Result<Int>) -> Void) { if

    !i.isMultiple(of: 2) { completion(.error(.isOdd)) return // ! } completion(.success(i / 2)) } 32
  16. func half(of i: Int) -> RxSwift.Single<Int> { if !i.isMultiple(of: 2)

    { return .error(Error.isOdd) } return .just(i / 2) } ͜ΕͳΒ੩తܕ෇͚ͷԸܙΛड͚ͭͭɺ ʮඇಉظͰ͋Δ͜ͱʯΛදݱͰ͖Δ 36
  17. • Promise(Future) ύλʔϯ • PromiseK • Hydra • RxSwift •

    etc • async/await • comming soon in Swift 5.x!? • ͜ͷݴޠ࢓༷Λϕʔεʹ Future ܕΛߏஙՄೳ CPSΛશ໘తʹஔ͖͔͑Մೳ͔ͱ͍͏ͱ೉͍͕͠ɺ બ୒ࢶΛ஌ͬͨ͏͑ͰύλʔϯΛબͼ͍ͨ 37
  18. ʮද໌ʯʹ͍ͭͯͷิ଍ • Ҿ਺ɾ໭Γ஋ͷܕ͚͕ͩද໌Ͱ͸ͳ͍ͱݴ͚ͬͨΕͲ… • assert ͚ͩͰͳ͘ɺSwiftͰݴ͑͹ generics ΍ conditional conformance΋ද໌ͷํ๏

    • ΂ͭʹɺίϯύΠϥղऍෆೳͳද໌͸ྑ͘ͳ͍…Θ͚Ͱ͸ͳ͍ • දݱྗΛ্͛Δͱֶशίετɾෳࡶੑ͕ϖΠ͠ͳ͍͜ͱ΋ଟ͍ͷͰɺͦ͜͸όϥϯε • ͨͱ͑͹ɺڽͬͨprotocolͷwhere۟ɺৗਓʹཧղग़དྷΔ͔ͬͯݴΘΕΔͱݫ͍͠ΑͶ… ɹ 40
  19. ʮܧଓ౉͠ʯʹ͍ͭͯͷิ଍ • ࣮͸ async/await͸ʮ಺෦తͳCPSม׵ʯͦͷ΋ͷͱ͍͑·͢ Continuation Passing Style Revisited – Fabulous

    Adventures In Coding https://blogs.msdn.microsoft.com/ericlippert/2010/10/21/ continuation-passing-style-revisited-part-one/ ͦͷ࿨༁ matarillo.com: ܧଓ౉͠ελΠϧ(CPS)ͱඇಉظߏจ(async/await) https://matarillo.com/general/cps 42
  20. Swiftͷ async/await ͱ ܧଓͱ Future ͷؔ܎ Chris Lattnerͷproposal2ʹΑΔͱɺ • ଞͷݴޠͰ͸

    Future ͕·ͣ͋ͬͯɺͦͷ্ʹasync/await͕৐͔͍ͬͬͯΔ • Swiftͷasync/awaitͷઃܭͰ͸ͦΕΛલఏͱ͸ͤͣɺܧଓΛͦͷ··ѻ͏खஈΛ࿐ग़͍ͤͯ͞ Δ • ͱ͸͍͑ Future ͱ͍͏ܕ෇͚͞Εͨঢ়ଶͰѻ͏ΠϯλϑΣʔε΋ߏஙՄೳͰ͢Αɺͱ͍͏͜ͱ 2 https://gist.github.com/lattner/429b9070918248274f25b714dcfc7619 yimajo͞Μͷʮasync/awaitݚڀಡຊʯ΋͋ΘͤͯͲ͏ͧ 43