-
-
Save rintaro/f90726a51e8371807667a5fc7ebdab07 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
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