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

Phantom Typeでコンパイル時に状態チェックする: shibuya.swift #4

Phantom Typeでコンパイル時に状態チェックする: shibuya.swift #4

kazuhiro4949

June 08, 2016
Tweet

More Decks by kazuhiro4949

Other Decks in Technology

Transcript

  1. 1IBOUPN5ZQFΛ࢖ͬͨ Ϋϥεఆٛ class PokemonStateType {} class Struggle: PokemonStateType {} //

    ઓಆঢ়ଶ class Calm: PokemonStateType {} // ٳܜঢ়ଶ class Pokemon<T: PokemonStateType> { static func use() -> Pokemon<Calm> { return Pokemon<Calm>() } func ready() -> Pokemon<Struggle> { return Pokemon<Struggle>() } } extension Pokemon where T: Struggle { func attack() {} }
  2. 1IBOUPN5ZQFΛ࢖ͬͨ Ϋϥεఆٛ class PokemonStateType {} class Struggle: PokemonStateType {} //

    ઓಆঢ়ଶ class Calm: PokemonStateType {} // ٳܜঢ়ଶ class Pokemon<T: PokemonStateType> { static func use() -> Pokemon<Calm> { return Pokemon<Calm>() } func ready() -> Pokemon<Struggle> { return Pokemon<Struggle>() } } extension Pokemon where T: Struggle { func attack() {} } ঢ়ଶΛܕύϥϝʔλͱͯ࣋ͭ͠ϙέϞϯΫϥεΛ࡞੒͢Δ
  3. 1IBOUPN5ZQFΛ࢖ͬͨ Ϋϥεఆٛ class PokemonStateType {} class Struggle: PokemonStateType {} //

    ઓಆՄೳঢ়ଶ class Calm: PokemonStateType {} // ઓಆෆՄঢ়ଶ class Pokemon<T: PokemonStateType> { static func use() -> Pokemon<Calm> { return Pokemon<Calm>() } func ready() -> Pokemon<Struggle> { return Pokemon<Struggle>() } } extension Pokemon where T: Struggle { func attack() {} } ϙέϞϯ͸ʮઓಆঢ়ଶʯͱʮٳܜঢ়ଶʯΛ࣋ͭ class PokemonStateType {} class Struggle: PokemonStateType {} // ઓಆঢ়ଶ class Calm: PokemonStateType {} // ٳܜঢ়ଶ class Pokemon<T: PokemonStateType> { static func use() -> Pokemon<Calm> { return Pokemon<Calm>() } func ready() -> Pokemon<Struggle> { return Pokemon<Struggle>() } } extension Pokemon where T: Struggle { func attack() {} }
  4. 1IBOUPN5ZQFΛ࢖ͬͨ Ϋϥεఆٛ class PokemonStateType {} class Struggle: PokemonStateType {} //

    ઓಆՄೳঢ়ଶ class Calm: PokemonStateType {} // ઓಆෆՄঢ়ଶ class Pokemon<T: PokemonStateType> { static func use() -> Pokemon<Calm> { return Pokemon<Calm>() } func ready() -> Pokemon<Struggle> { return Pokemon<Struggle>() } } extension Pokemon where T: Struggle { func attack() {} } ઓಆঢ়ଶͷ࣌͸BUUBDL ͕࣮ߦՄೳ class PokemonStateType {} class Struggle: PokemonStateType {} // ઓಆঢ়ଶ class Calm: PokemonStateType {} // ٳܜঢ়ଶ class Pokemon<T: PokemonStateType> { static func use() -> Pokemon<Calm> { return Pokemon<Calm>() } func ready() -> Pokemon<Struggle> { return Pokemon<Struggle>() } } extension Pokemon where T: Struggle { func attack() {} }
  5. 1PLFNPOΫϥεΛ࢖ͬͯΈΔ let pokemon = Pokemon.create() pokemon.attack() // -> ίϯύΠϧ࣌ʹΤϥʔ pokemon.ready().attack()

    // -> ͬͪ͜͸ίϯύΠϧΤϥʔ͕ग़ͳ͍ ܕύϥϝʔλʹ$BMN͕࣋ͭ৔߹ɺBUUBDL ϝιο υ͸ݺ΂ͳ͍ let pokemon = Pokemon.use() pokemon.attack() // -> ίϯύΠϧ࣌ʹΤϥʔ pokemon.ready().attack() // -> ͬͪ͜͸ίϯύΠϧΤϥʔ͕ग़ͳ͍
  6. 1PLFNPOΫϥεΛ࢖ͬͯΈΔ let pokemon = Pokemon.create() pokemon.attack() // -> ίϯύΠϧ࣌ʹΤϥʔ pokemon.ready().attack()

    // -> ͬͪ͜͸ίϯύΠϧΤϥʔ͕ग़ͳ͍ ܕύϥϝʔλʹ4USVHHMFΛ࣋ͭ1PLFNPOͷ৔߹ɺ BUUBDL ϝιου͕ݺ΂Δ let pokemon = Pokemon.use() pokemon.attack() // -> ίϯύΠϧ࣌ʹΤϥʔ pokemon.ready().attack() // -> ͬͪ͜͸ίϯύΠϧΤϥʔ͕ग़ͳ͍
  7. ࢀߟࢿྉ w 4XJGUͰ1IBOUPN5ZQF ༓ྶܕ  w IUUQRJJUBDPNUBLFUPJUFNTFGEFDEF w 'VODUJPOBM4OJQQFU1IBOUPN5ZQFT w

    IUUQTXXXPCKDJPCMPHGVODUJPOBMTOJQQFUQIBOUPNUZQFT w LB[VIJSP4USJOH4UZMJ[FS w IUUQTHJUIVCDPNLB[VIJSP4USJOH4UZMJ[FS