Created
August 29, 2015 21:20
-
-
Save gjlondon/6303342d4f07fe8184a2 to your computer and use it in GitHub Desktop.
Confusion on Swift protocols and types
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
// example of confusion with swift protocols and classes | |
protocol Bar { | |
var m: Int { get set } | |
} | |
class Foo:Bar { | |
var m: Int | |
required init(m: Int) { | |
self.m = m | |
} | |
} | |
class Baz: Bar { | |
var m: Int | |
required init(m: Int) { | |
self.m = m | |
} | |
} | |
class Cog: Foo { | |
required init(m: Int) { | |
super.init(m: m) | |
self.m = m | |
} | |
} | |
var t1 = Foo.self | |
println(t1) | |
var c1 = t1(m: 3) | |
println(c1) | |
// doesn't work: "Cannot assign a value of type Baz.Type to value of type Foo.Type" | |
t1 = Baz.self | |
// doesn't work: "Foo.Type does not conform to protocol Bar" | |
var t2: Bar = Foo.self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment