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

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

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

Yuka Ezura

March 22, 2017
Tweet

More Decks by Yuka Ezura

Other Decks in Programming

Transcript

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

    func common() { /* do something */ } func method1() { /* do something */ } func method2() { /* do something */ } }
  2. 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 */ }
  3. ࣮૷ͷ֓೦ਤ instance Init state instance State1 instance State2 func method1

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

    { let object = Object<State2>(…) // Object ͷϓϩύςΟΛҾ͖ܧ͗ return object } }
  5. Object<Init> ͔Β Object<State1> ΁
 σʔλΛҾ͖ܧ͙ʹ͸ʁʁ new.name = self.name new.something =

    self.something struct Container { var name: String var something: Any }
  6. 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
  7. extension Object where State: Init { init(name: String, something: Any)

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

    ͜Ε͕ՄೳͱͳΔ৚͕݅͋Γ·ׂ͕͢Ѫ͠·͢
 (৚͕݅ἧ͍ͬͯͳ͍৔߹ɺίϯύΠϧΤϥʔ)
  9. ࢀর • ۽୩ͱ៸໘͕ϓϩάϥϛϯάίʔυͷ಺͔Βௌ͑ͯ͘͜ Δ੠ʹࣖΛ܏ָ͚ͯ͠ΉϥδΦ #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