Last active
February 13, 2016 00:31
-
-
Save AliSoftware/e6b931c1731f016e41fb to your computer and use it in GitHub Desktop.
Use of "Self" vs "self" in "static var" + generic protocol implementation
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 Fooable { | |
static var staticBigSelf: String { get } | |
static var staticSmallSelf: String { get } | |
var instanceBigSelf: String { get } | |
var instanceSmallSelf: String { get } | |
init() | |
} | |
extension Fooable { | |
static var staticBigSelf: String { return String(Self) } | |
static var staticSmallSelf: String { return String(self) } | |
var instanceBigSelf: String { return String(Self) } | |
var instanceSmallSelf: String { return String(self) } | |
} | |
class ParentView: Fooable { | |
required init() {} | |
} | |
class ChildView: ParentView { | |
required init() {} | |
} | |
func fooableFactory<T where T: Fooable>() -> T { | |
print("Creating instance of \(T.self) with Self=\(T.staticBigSelf) & self=\(T.staticSmallSelf)") | |
return T() | |
} | |
func dump<T where T: Fooable>(instance: T) { | |
print("instance \(instance) with Self=\(instance.instanceBigSelf) & self=\(instance.instanceSmallSelf)") | |
} | |
let cv = fooableFactory() as ChildView // Prints: Creating instance of ChildView with Self=ParentView & self=ChildView | |
dump(cv) // Prints: instance ChildView with Self=ChildView & self=ChildView | |
ChildView.staticBigSelf // Prints: "ChildView" | |
ChildView.staticSmallSelf // Prints: "ChildView" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference, this has been duly reported as Swift bug SR-617 in JIRA