Re: エラーと警告でコードをデザインする

Re: エラーと警告でコードをデザインする

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

March 22, 2017
Tweet

Transcript

  1. Re: ΤϥʔͱܯࠂͰ ίʔυΛσβΠϯ͢Δ 2017/3/22 ezura

  2. ςετ ։ൃମ੍ɾ؀ڥ ઃܭࢥ૝ɾཧ࿦ ݴޠʹΑΔ
 αϙʔτ j j j j ϜϲεϮϥ΀ᩆ౰ᕥ

  3. https://speakerdeck.com/ezura/eratojing-gao-de-kodowodezainsuru-chi- tohuang-falselun-wu-qu ΤϥʔͱܯࠂͰίʔυΛσβΠϯ͢Δ NFUIPE΀Ṻ΅ᲇͩᘡᔀ͞ᩆ͖ͩͥͺΤ
 θϸϖΩϰᙉͽݕᒕͫΝ

  4. લఏ struct Object { var name: String var something: Any

    func common() { /* do something */ } func method1() { /* do something */ } func method2() { /* do something */ } }
  5. method1 Λ࣮ߦ͔ͯ͠ΒͷΈ
 method2 Λ࣮ߦՄೳ (ҧ൓ΛίϯύΠϧ࣌ʹݕ஌) Mission1 // OK object.method1().method2() //

    Error object.method2()
  6. method1, method2 Λ࣮ߦޙͷ
 ΠϯελϯεͷΈҾ਺ͱͯ͠ड͚औΔؔ਺ (ҧ൓ΛίϯύΠϧ࣌ʹݕ஌) Mission2 // OK accept(object.method1().method2()) //

    Error accept(object.method2())
  7. ࣮૷

  8. ࣮૷ͷ֓೦ਤ instance Init state instance State1 instance State2 func method1

    func method2
  9. struct Object<State> { var name: String var something: Any func

    common() { /* do something */ } }
  10. extension Object where State: Init { func method1() -> Object<State1>

    { /* do something */ } } extension Object where State: State1 { func method2() -> Object<State2> { /* do something */ } } func accept(_ object: Object<State2>) { /* do something */ }
  11. ࣮૷ͷ֓೦ਤ instance Init state instance State1 instance State2 func method1

    func method2 Object<Init> Object<State1> Object<State2>
  12. ཧ࿦͸Θ͔ͬͨʂ ࣮૷ʹམͱ͠ࠐΜͰΈΑ͏

  13. extension Object where State: Init { func method1() -> Object<State2>

    { let object = Object<State2>(…) // Object ͷϓϩύςΟΛҾ͖ܧ͗ return object } }
  14. ܕύϥϝʔλ͕ҧ͏Πϯελϯε
 (= ҟͳΔܕ)
 ͷؒͰͲ͏΍ͬͯσʔλΛҾ͖ܧ͝͏…

  15. Object<Init> ͔Β Object<State1> ΁
 σʔλΛҾ͖ܧ͙ʹ͸ʁʁ new.name = self.name new.something =

    self.something struct Container { var name: String var something: Any }
  16. ΩϟετͰ͖ΔΑʂ ۽୩͞Μ

  17. Object<Init> ͔Β Object<State1> ΁ extension Object where State: Init {

    func method1() -> Object<State2> { let object = unsafeBitCast(self, to: Object<State2>.self) // do something return object } } Object<Init> ͔Β Object<State1> ΁ Cast
  18. ࣮૷Ͱ͖ͨʂ ࢖͍উख͸Ͳ͏͔ͳʁʁ

  19. let object = Object<Init>(name: "sample", something: "something") <Init> ͷ໌͕ࣔඞཁ…
 (´ɾωɾʆ)

  20. ίϯύΠϥʹ఻͑ͯΈΑ͏

  21. extension Object where State: Init { init(name: String, something: Any)

    { self.name = name self.something = something } } State ͕ Init ͷͱ͖ͷΈ
 ίϯετϥΫλΛએݴ
  22. let object = Object(name: "sample", something: "something") <Init> ͷ໌͕ࣔෆཁ ஫:

    ͜Ε͕ՄೳͱͳΔ৚͕݅͋Γ·ׂ͕͢Ѫ͠·͢
 (৚͕݅ἧ͍ͬͯͳ͍৔߹ɺίϯύΠϧΤϥʔ)
  23. ͱͬͯ΋៉ྷʹͳΓ·ͨ͠ʂʂ
 ٩( 'ω' )و

  24. https://gist.github.com/ezura/ e71db2b0471285061f9700809f92f2b1 ίʔυ

  25. ࢀর • ۽୩ͱ៸໘͕ϓϩάϥϛϯάίʔυͷ಺͔Βௌ͑ͯ͘͜ Δ੠ʹࣖΛ܏ָ͚ͯ͠ΉϥδΦ #9, #14
 http://mookmookradio.com/a0005/ • where Ͱͷܕਪ࿦ͷఏএऀͷํͷ

    gist
 https://gist.github.com/tarunon/ d7071689ab55e29bd367a44833687aa3 • ΤϥʔͱܯࠂͰίʔυΛσβΠϯ͢Δ
 https://speakerdeck.com/ezura/eratojing-gao-de- kodowodezainsuru-chi-tohuang-falselun-wu-qu