Last active
August 29, 2015 14:05
-
-
Save austinzheng/4dd54dce67574d7060a4 to your computer and use it in GitHub Desktop.
Reification example
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
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