Created
May 6, 2016 12:13
-
-
Save ktanaka117/d3ef1f304ef1ce0103b16bbc40e4d882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import Foundation | |
enum Element { | |
case Water, Fire, Thunder | |
} | |
protocol Magic { | |
typealias MagicType | |
func chant(element: MagicType) | |
} | |
// これができない | |
//class Mage: Magic { | |
// func chant(element: MagicType) {} | |
//} | |
class AnyMage <MagicType>: Magic { | |
let _chant: MagicType -> Void | |
required init<U: Magic where U.MagicType == MagicType> (mage: U) { | |
self._chant = mage.chant | |
} | |
func chant(element: MagicType) { | |
chant(element) | |
} | |
} | |
class WaterMage: Magic { | |
func chant(element: Element) { print("💦") } | |
} | |
class FireMage: Magic { | |
func chant(element: Element) { print("🔥") } | |
} | |
let waterMage = AnyMage(mage: WaterMage()) | |
let fireMage = AnyMage(mage: FireMage()) | |
// なぜできない!!! | |
//waterMage.chant(.Water) | |
//fireMage.chant(.Fire) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment