Created
October 31, 2015 05:54
-
-
Save nikita-leonov/371c17eb23e582dc20e6 to your computer and use it in GitHub Desktop.
`Generic` Protocols
This file contains hidden or 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
| class Container<T> { | |
| private var containedValue: T | |
| init(_ value: T) { | |
| containedValue = value | |
| } | |
| } | |
| protocol ContainerProtocol { | |
| typealias ValueType | |
| var value: ValueType { get set } | |
| } | |
| extension Container: ContainerProtocol { | |
| typealias ValueType = T | |
| var value: ValueType { | |
| get { | |
| return self.containedValue | |
| } | |
| set { | |
| self.containedValue = newValue | |
| } | |
| } | |
| } | |
| class App<T: ContainerProtocol where T.ValueType == Int> { | |
| var container: T | |
| init(_ container: T) { | |
| self.container = container | |
| } | |
| } | |
| App(Container(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment