Skip to content

Instantly share code, notes, and snippets.

@rintaro
Last active October 6, 2017 07:57
Show Gist options
  • Save rintaro/f90726a51e8371807667a5fc7ebdab07 to your computer and use it in GitHub Desktop.
Save rintaro/f90726a51e8371807667a5fc7ebdab07 to your computer and use it in GitHub Desktop.
protocol CatProtocol {
var name: String { get set }
func nya() -> String
mutating func sleep()
func clone() -> Self
func cast<T>(to: T.Type) -> T?
}
class Cat : CatProtocol {
required init() {}
var name: String = "tama"
func nya() -> String { return "nyaaaa" }
func sleep() { print("zzz...") }
func clone() -> Self {
return type(of: self).init()
}
func cast<T>(to: T.Type) -> T? {
return self as? T
}
}
struct AnyCat1 : CatProtocol {
private var base: CatProtocol
init(_ x: CatProtocol) {
base = x
}
var name: String {
get { return base.name }
set { base.name = newValue }
}
func nya() -> String {
return base.nya()
}
mutating func sleep() { base.sleep() }
func clone() -> AnyCat1 {
return AnyCat1(base.clone())
}
func cast<T>(to: T.Type) -> T? {
return base.cast(to: to)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment