Naming Things: A Cautionary Tale
There is an edge case with protocol extensions that may prove to be a great source of frustration if you are not careful. In the previous section, you added description to the requirements of Exercise (by making Exercise inherit from the CustomStringConvertible protocol), added a default implementation for description, and added a specific implementation to TreadmillWorkout that took precedence over the default implementation. This worked correctly because description was required by the Exercise protocol. But what happens if you write a protocol extension to add a property or method, then add a property or method with the same name (but a different implementation) to a conforming type?
The answer is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access