Skip to content

Instantly share code, notes, and snippets.

@austinzheng
Last active August 29, 2015 14:05
Show Gist options
  • Save austinzheng/4dd54dce67574d7060a4 to your computer and use it in GitHub Desktop.
Save austinzheng/4dd54dce67574d7060a4 to your computer and use it in GitHub Desktop.
Reification example
protocol InitProtocol { init() }
struct SomeStruct<T: InitProtocol> {
var x: T? = nil
mutating func instantiate() {
if x == nil {
x = T()
}
}
}
struct Foo : InitProtocol {
func favoriteString() -> String {
return "reify me please"
}
}
struct Bar : InitProtocol {
func favoriteString() -> String {
return "another string"
}
}
var a = SomeStruct<Foo>()
var b = SomeStruct<Bar>()
a.instantiate()
b.instantiate()
(a.x?.favoriteString())! // prints out "reify me please"
(b.x?.favoriteString())! // prints out "another string"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment