Skip to content

Instantly share code, notes, and snippets.

@nikita-leonov
Created October 31, 2015 05:54
Show Gist options
  • Save nikita-leonov/371c17eb23e582dc20e6 to your computer and use it in GitHub Desktop.
Save nikita-leonov/371c17eb23e582dc20e6 to your computer and use it in GitHub Desktop.
`Generic` Protocols
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