I can only guess as to why isEqualTo:
is a required method for the drawable protocol. Perhaps so that whatever is drawing these things never wastes time drawing the same thing twice?
I can comment on the rest of it however.
Equatable
is a Swift protocol (not available in Objective-C) which requires that there is a ==
operator defined for the type.
In Objective-C, there is no operator overloading. Moreover, in Objective-C, using ==
to compare objects simply compares their pointers. If the objects are the same object (same memory location), ==
returns true. If we want to see if the objects are different objects but still considered equal, we must use isEqualTo:
. This is a method defined by NSObject
and the default implementation simply returns the result of ==
comparison. But classes tend to override this.
In Swift, ==
has different behavior. In Swift, ==
returns behaves similarly to how we expect the isEqualTo:
method to behave in Objective-C. That's because Swift has the ===
operator for comparing references. ===
returns true if these objects are the same (same memory location), but ==
is a custom implemented method that determines whether the objects are considered equal even if they are in different memory locations.
So I'm guessing the Drawable
protocol has Objective-C classes in mind when it declares isEqualTo:
as one of its required methods.
We could alternatively write the Drawable
protocol as such:
protocol Drawable: Equatable {
func draw()
}
From a strictly Swift perspective, this is a roughly equivalent protocol definition. But this means that whoever is using Drawable
objects expects to compare them with ==
in Swift rather than isEqualTo:
. And moreover, this means if we want to use any Objective-C defined objects with the protocol, now we must implement a custom ==
function for each of them, which most likely looks like this:
func == (left ObjCClass, right ObjCClass) -> Bool {
return left.isEqualTo(right)
}
But we have to do this for every Objective-C class we want to define as Drawable
.
The alternative is to define our protocol as you presented it, using isEqualTo:
, a very commonplace Objective-C method.
Now, to make all of our Swift types conform to Drawable
, all we have to do is implement the draw()
method and conform to Equatable
. As long as we conform to Equatable
, the extension will add the isEqualTo:
method to our Swift type as a simple return left == right
effectively (and the existence of the ==
method is what the Equatable
protocol guarantees).