Continuation-Passing Style and Design By Contract(English ver.) / 20190319(E) #tryswift_pre

704056da9a4c4e075ad14479beaebab7?s=47 takasek
March 19, 2019

Continuation-Passing Style and Design By Contract(English ver.) / 20190319(E) #tryswift_pre

Presentation in
【増枠/increase】try! Swift Pre Talks 2019 - connpass
https://tryswifttokyo-aftertalks.connpass.com/event/120987/

Japanese ver.
https://speakerdeck.com/takasek/20190319-number-tryswift-pre

704056da9a4c4e075ad14479beaebab7?s=128

takasek

March 19, 2019
Tweet

Transcript

  1. 3.

    ʮiOSΞϓϦઃܭύλʔϯೖ໳ʯ ON SALE!! https://peaks.cc/takasek/iOS_architecture takasek୲౰ষ Chap 1ʮTo designʯ Chap 2ʮBefore

    applying patterns to your designʯ Chap 3ʮDesigning swiftyʯ Chap 4ʮOverview through history of architecturesʯ 3
  2. 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
  3. 6.

    ! 6

  4. 10.
  5. 13.

    Design By Contract Asserts; • Preconditionɿ a caller should ensure

    it. described by Argument Type or so on. • Postconditionɿ a callee should ensure it. described by Return Type or so on. If precondition is violated, throw Exception. 13
  6. 14.

    For example, func half(of i: Int) throws -> Int {

    assert(i.isMultiple(of: 2)) if !i.isMultiple(of: 2) { throw Error.isOdd } return i / 2 } • Precondition: • accepts Int. • accepts Even. (not describable as type) • Postcondition: returns Int. • Exception: thrown if the argument is odd. 14
  7. 17.

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

    2) { throw Error.isOdd } return i / 2 } translate it to CPS. 17
  8. 18.

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

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

    19

  10. 21.

    21

  11. 23.

    23

  12. 25.

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

    !i.isMultiple(of: 2) { return // ! } completion(i / 2) } 25
  13. 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
  14. 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
  15. 29.

    29

  16. 31.

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

    !i.isMultiple(of: 2) { completion(.error(.isOdd)) // ! } completion(.success(i / 2)) } 31
  17. 32.

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

    !i.isMultiple(of: 2) { completion(.error(.isOdd)) return // ! } completion(.success(i / 2)) } 32
  18. 36.

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

    { return .error(Error.isOdd) } return .just(i / 2) } ͜ΕͳΒ੩తܕ෇͚ͷԸܙΛड͚ͭͭɺ ʮඇಉظͰ͋Δ͜ͱʯΛදݱͰ͖Δ 36
  19. 37.

    • Promise(Future) ύλʔϯ • PromiseK • Hydra • RxSwift •

    etc • async/await • comming soon in Swift 5.x!? • ͜ͷݴޠ࢓༷Λϕʔεʹ Future ܕΛߏஙՄೳ CPSΛશ໘తʹஔ͖͔͑Մೳ͔ͱ͍͏ͱ೉͍͕͠ɺ બ୒ࢶΛ஌ͬͨ͏͑ͰύλʔϯΛબͼ͍ͨ 37
  20. 40.

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

    • ΂ͭʹɺίϯύΠϥղऍෆೳͳද໌͸ྑ͘ͳ͍…Θ͚Ͱ͸ͳ͍ • දݱྗΛ্͛Δͱֶशίετɾෳࡶੑ͕ϖΠ͠ͳ͍͜ͱ΋ଟ͍ͷͰɺͦ͜͸όϥϯε • ͨͱ͑͹ɺڽͬͨprotocolͷwhere۟ɺৗਓʹཧղग़དྷΔ͔ͬͯݴΘΕΔͱݫ͍͠ΑͶ… ɹ 40
  21. 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
  22. 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