Skip to content

Instantly share code, notes, and snippets.

@filimo
Last active February 21, 2020 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filimo/639b74d516c26bc2b719f326a352eace to your computer and use it in GitHub Desktop.
Save filimo/639b74d516c26bc2b719f326a352eace to your computer and use it in GitHub Desktop.
protocol ProtocolB1 {
init()
func methodB1()
}
extension ProtocolB1 where Self: ClassB1 {
func methodB1() {
print("ProtocolB1 where Self: ClassB1", #function, #line, Self.self)
}
}
extension ProtocolB1 {
func methodB1() {
print("ProtocolB1", #function, #line, Self.self)
}
}
protocol ProtocolB2: ProtocolB1 {
associatedtype T
func methodB2() -> T
}
extension ProtocolB2 where Self == ClassB1 {
func methodB2() -> Self {
print("ProtocolB", #function, #line, Self.self)
return Self()
}
}
class ClassB1: ProtocolB2 {
required init() {}
}
class ClassB2: ProtocolB2 {
required init() {}
func methodB2() -> ClassB2 {
print(#function, #line, Self.self)
return Self()
}
}
class ClassB3: ProtocolB2 {
required init() {}
func methodB1() {
print(#function, #line, Self.self)
}
func methodB2() -> ClassB3 {
print(#function, #line, Self.self)
return Self()
}
}
var itemsB1: [ProtocolB1] = [ClassB1(), ClassB2(), ClassB3()]
for item in itemsB1 { item.methodB1() }
print("------------------")
var itemsB2: some Collection = [ClassB1(), ClassB2(), ClassB3()]
for item in itemsB2 {
if let item = item as? ProtocolB1 {
item.methodB1()
}
if let item = item as? ClassB1 {
item.methodB2()
}
if let item = item as? ClassB2 {
item.methodB2()
}
if let item = item as? ClassB3 {
item.methodB2()
}
}
// ProtocolB1 where Self: ClassB1 methodB1() 11 ClassB1
// ProtocolB1 methodB1() 17 ClassB2
// methodB1() 51 ClassB3
// ------------------
// ProtocolB1 where Self: ClassB1 methodB1() 11 ClassB1
// ProtocolB methodB2() 29 ClassB1
// ProtocolB1 methodB1() 17 ClassB2
// methodB2() 42 ClassB2
// methodB1() 51 ClassB3
// methodB2() 55 ClassB3
protocol ProtocolB1 {
init()
func methodB1()
}
extension ProtocolB1 where Self: ClassB1 {
func methodB1() {
print("ProtocolB1 where Self: ClassB1", #function, Self.self)
}
}
extension ProtocolB1 {
func methodB1() {
print("ProtocolB1", #function, Self.self)
}
}
protocol ProtocolB2: ProtocolB1 {
associatedtype T
func methodB2() -> T
}
extension ProtocolB2 where Self == ClassB1 {
func methodB2() -> Self {
print("ProtocolB", #function, Self.self)
return Self()
}
}
class ClassB1: ProtocolB2 {
required init() {}
}
class ClassB2: ProtocolB2 {
required init() {}
func methodB2() -> ClassB2 {
print(#function, Self.self)
return Self()
}
}
var itemsB1: [ProtocolB1] = [ClassB1(), ClassB2()]
for item in itemsB1 { item.methodB1() }
print("------------------")
var itemsB2: some Collection = [ClassB1(), ClassB2()]
for item in itemsB2 {
if let item = item as? ProtocolB1 {
item.methodB1()
}
if let item = item as? ClassB1 {
item.methodB2()
}
if let item = item as? ClassB2 {
item.methodB2()
}
}
// ProtocolB1 where Self: ClassB1 methodB1() ClassB1
// ProtocolB1 methodB1() ClassB2
// ------------------
// ProtocolB1 where Self: ClassB1 methodB1() ClassB1
// ProtocolB methodB2() ClassB1
// ProtocolB1 methodB1() ClassB2
// methodB2() ClassB2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment