April 2026
Intermediate
683 pages
25h 29m
English
In previous chapters, you learned how generics increase the flexibility of any data type. Protocols are no exception; generics can increase the abstraction degree of any protocol. In this section, you’ll learn how generics apply to protocols.
So far, protocol examples have contained concrete data types, such as String or Double. However, fixating on data types might be a limiting factor, leading to the creation of multiple similar protocols.
Check the sample in Listing 10.20. StringReversible and IntReversible are very similar protocols, with a slight data type difference: StringReversible.reverse is expected to turn "ABC" into "CBA", while IntReversible.reverse is expected to turn 123 into 321.
protocol StringReversible ...
Read now
Unlock full access