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

704056da9a4c4e075ad14479beaebab7?s=47 takasek
March 19, 2019

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

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

704056da9a4c4e075ad14479beaebab7?s=128

takasek

March 19, 2019
Tweet

Transcript

  1. ܧଓ౉͠ͱܖ໿ʹΑΔઃܭ Continuation-Passing Style and Design By Contract by. 2019/3/19 try!

    Swift Pre Talks 2019 1
  2. takasek @takasek Works OSS: ActionClosurable౳ App: PasteTheType Articles ʮίϯύΠϧΤϥʔ΍ϥϯλΠϜΤϥʔΛ௚͍ͯ͘͠ ͚ͩͰiOSΞϓϦͷ࡞Γํ͕Θ͔ΔϓϩδΣΫτʯ

    ʮ͓લΒ͕ModelͱݺͿΞϨΛͳΜͱݺͿ΂͖͔ʯ ʮiOSΞϓϦઃܭύλʔϯೖ໳ʯʢڞஶʣ 2
  3. ʮiOSΞϓϦઃܭύλʔϯೖ໳ʯ ޷ධൃചதʂ https://peaks.cc/takasek/iOS_architecture takasek୲౰ষ ୈ1ষʮઃܭ͢Δͱ͍͏͜ͱʯ ୈ2ষʮઃܭʹύλʔϯΛద༻͢Δલʹʯ ୈ3ষʮSwiftΒ͘͠ઃܭ͢Δʯ ୈ4ষʮΞʔΩςΫνϟͷύλʔϯΛௗᛌ͢ Δʯ 3

  4. ܧଓ౉͠ Continuation- Passing Style(CPS) 4

  5. // 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
  6. ! 6

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

    ͜ͷΠϯλϑΣʔεʹ͸ɺخ͘͠ͳ͍఺͕͋Γ·͢ 7
  8. ʮ͋ͬɺ࿩͸ಡΊͨʯ 8

  9. ʮίʔϧόοΫ஍ࠈͷ࿩Ͱ͠ΐʯ 9

  10. ҧ͍·͢ 10

  11. ܖ໿ʹΑΔ ઃܭ Design By Contract 11

  12. ܖ໿ʹΑΔઃܭ Design By Contract ΦϒδΣΫτࢦ޲Λ޿Ίͨݚڀऀ όʔτϥϯυɾϝΠϠʔʹΑΔɺ ৴པੑͷߴ͍γεςϜΛ ߏங͢ΔͨΊͷΞΠσΞ 12

  13. ܖ໿ʹΑΔઃܭ ͋Δϧʔνϯ͕ɺݺ͹ΕΔ૬खͱͷؒͰऔΓަΘܾ͢Ί͝ͱΛʮද໌ʯ͢Δ • ࣄલ৚݅ɿ ݺͿଆ͕͜ͷ৚݅Λอূ͢Δ৔߹ͷΈɺϧʔνϯ͸ॲཧΛਖ਼͘͠ߦ͏ Ҿ਺ͷܕͳͲͰදݱՄೳ • ࣄޙ৚݅ɿ ࣄલ৚͕݅कΒΕ͍ͯͨΒɺϧʔνϯͷ࣮ߦ݁Ռ͕อূ͢Δ৚݅ ໭Γ஋ͷܕͳͲͰදݱՄೳ

    ࣄલ৚͕݅ഁΒΕɺࣄޙ৚݅ΛकΕͳ͍৔߹͸ྫ֎͕ൃੜ͢Δ • ෆม৚݅ ʹ͍ͭͯ͸ࠓճ͸লུ 13
  14. ίʔυͱද໌ͷ۩ମྫ 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
  15. ʮܖ໿ʹΑΔઃܭʯͷ࣮ݱͷͨΊʹɺSwiftͷܕ෇͚͸େ͖͘໾ཱ͍ͬͯΔ 15

  16. ͯ͞ 16

  17. func half(of i: Int) throws -> Int { if !i.isMultiple(of:

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

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

  20. OK, ح਺ͷ৔߹΋ ֬ೝ͠Α͏ʂ 20

  21. 21

  22. ݺ͹Εͳ͍…ʁ 22

  23. 23

  24. completionͷ ݺͼ࿙Ε 24

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

    !i.isMultiple(of: 2) { return // ! } completion(i / 2) } 25
  26. 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
  27. 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
  28. ௚ͬͨʂ ΋͏Ұ౓ڍಈΛ ֬ೝ͠Α͏ʂ 28

  29. 29

  30. 2౓ݺ͹Ε͍ͯΔ …ͩͱ…ʁ 30

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

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

    !i.isMultiple(of: 2) { completion(.error(.isOdd)) return // ! } completion(.success(i / 2)) } 32
  33. ܧଓ౉͠ελΠϧΛ࢖͏ͱ ੩తܕ෇͚ͷԸܙΛ ड͚ΒΕͳ͍ 33

  34. ͳͥͳͷ͔ ໭Γ஋ͷܕʹΑͬͯ ࣄޙ৚݅ΛදݱͰ͖ͳ͍͔Β (T, (U) -> Void) -> Void Void͸࠷ऑͷࣄޙ৚݅

    34
  35. Ͳ͏͢Ε͹͍͍͔ ໭Γ஋ͷܕʹΑͬͯ ࣄޙ৚݅Λදݱ͢Ε͹͍͍ (T) -> XXX<U> ʮ͍ͣΕ݁Ռ͕ಘΒΕΔʯ͜ͱΛܕ෇͚ͰࣔͤΕ͹ɺ ඇಉظॲཧͰ΋໭Γ஋Λػೳͤ͞ΒΕΔ 35

  36. func half(of i: Int) -> RxSwift.Single<Int> { if !i.isMultiple(of: 2)

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

    etc • async/await • comming soon in Swift 5.x!? • ͜ͷݴޠ࢓༷Λϕʔεʹ Future ܕΛߏஙՄೳ CPSΛશ໘తʹஔ͖͔͑Մೳ͔ͱ͍͏ͱ೉͍͕͠ɺ બ୒ࢶΛ஌ͬͨ͏͑ͰύλʔϯΛબͼ͍ͨ 37
  38. ݁࿦ ɾਓؒ͸ϛεΛ͠·͢ ɾϛεΛ͢Δલʹɺ ද໌ΛίϯύΠϥ͕෼͔ΔܗͰॻ͚ͳ͍͔ ҙࣝ͠ͳ͕Βઃܭ͠·͠ΐ͏ 38

  39. ͓·͚ ͱͯ΋5෼Ͱ͸ޠΓ͖Εͳ͍࿩ 39

  40. ʮද໌ʯʹ͍ͭͯͷิ଍ • Ҿ਺ɾ໭Γ஋ͷܕ͚͕ͩද໌Ͱ͸ͳ͍ͱݴ͚ͬͨΕͲ… • assert ͚ͩͰͳ͘ɺSwiftͰݴ͑͹ generics ΍ conditional conformance΋ද໌ͷํ๏

    • ΂ͭʹɺίϯύΠϥղऍෆೳͳද໌͸ྑ͘ͳ͍…Θ͚Ͱ͸ͳ͍ • දݱྗΛ্͛Δͱֶशίετɾෳࡶੑ͕ϖΠ͠ͳ͍͜ͱ΋ଟ͍ͷͰɺͦ͜͸όϥϯε • ͨͱ͑͹ɺڽͬͨprotocolͷwhere۟ɺৗਓʹཧղग़དྷΔ͔ͬͯݴΘΕΔͱݫ͍͠ΑͶ… ɹ 40
  41. ิ଍ͷิ଍ ٯʹݴ͑͹ɺ ܖ໿ʹΑΔઃܭ͸ ݴޠΛ໰Θͣద༻Մೳͳ֓೦Ͱ͢ t_wada͞ΜʹΑΔ ͋͑ͯPHPΛϕʔεʹͨ͠εϥΠυ PHPer͡Όͳͯ͘΋ΦεεϝͰ͢ https://speakerdeck.com/twada/php- conference-2016 41

  42. ʮܧଓ౉͠ʯʹ͍ͭͯͷิ଍ • ࣮͸ 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
  43. 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
  44. ؔ࿈ɿʮܖ໿ʹΑΔઃ ܭʯΛΩʔϫʔυͰ֦ ு͢Δ࿩ ʮܖ໿ʹΑΔઃܭʯʹ͍ͭͯৄղ͞Ε ͍ͯΔʮΦϒδΣΫτࢦ޲ೖ໳ʯʹ ͸ɺ ඇಉظʢฒߦʣॲཧΛ΋ͱʹ֦ுͨ͠ ৔߹ͷܖ໿ͷϙϦγʔʹ͍ͭͯͷٞ࿦ ΋͋Γ·͢ ໘ന͍Αʂ

    44