I've seen the answer to this Swift Equatable Protocol question that mentions how the ==
method must be declared in the global scope.
If I don't adopt Equatable
, I still could declare ==
to test for equality between two of my types.
// extension Foo: Equatable {}
func ==(lhs: Foo, rhs: Foo) -> Bool {
return lhs.bar == rhs.bar
}
struct Foo {
let bar:Int
}
The fact that its implementation needs to be declared at a global scope, makes it seem incidental to and distinct from a protocol, even if Equatable
was adopted.
How is the Equatable
protocol anything more than syntactic sugar that merely lets (us and) the compiler safely know that our type implemented the required method of the protocol?
Why does the operator implementation have to be globally declared, even for a protocol? Is this due to some different way that an operator is dispatched?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…