Here is the code
protocol Hello {
func hello()
}
extension Hello {
func hello() {
print("hello")
}
}
protocol Namable {
var name: String { get }
}
extension Namable {
var name: String {
return "wang"
}
}
extension Hello where Self: Namable {
func hello() {
print("hello, \(name)")
}
}
class A: Hello {
}
class B: A, Namable {
}
B().hello()
For this code, we have two implementations for the method Hello#hello()
, one is in the extension Hello
, the other is in the extension Hello where Self: Namable
, so for class B: A, Namable
, when we call func hello()
, which implementation it will take ? in this example, swift will call the implementation in extension Hello where Self: Namable
, but I'm not sure if it is take that one purposely, or randomly. Because it doesn't work this way in my other example.
Does anyone knows how it works ?