Protocol Extension where Clauses
Extensions allow you to add new methods and computed properties to any type, not just types you have defined. Likewise, protocol extensions allow you to add new methods and computed properties to any protocol. However, as we said earlier, the properties and methods you add in a protocol extension can only use other properties and methods that are guaranteed to exist.
Do you remember the built-in protocol Sequence from Chapter 22?
It has an associatedtype named Iterator, which must itself conform to IteratorProtocol.
And IteratorProtocol has an associatedtype named Element that indicates the type of elements produced by the generator.
When writing a protocol extension on Sequence, there are not ...
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