Skip to content

Instantly share code, notes, and snippets.

@gjlondon
Created August 29, 2015 21:20
Show Gist options
  • Save gjlondon/6303342d4f07fe8184a2 to your computer and use it in GitHub Desktop.
Save gjlondon/6303342d4f07fe8184a2 to your computer and use it in GitHub Desktop.
Confusion on Swift protocols and types
// 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