Chapter 4. Generics
I received a lot of feedback about protocol-oriented programming after the first version of this book was released. Almost all the feedback was very positive, however there was one conversation that I had, with one of the smartest people that I have had the privilege to meet, about what protocol-oriented programming was. One of the comments that he made was that I should not forget about generic programming. The conversation that we had about generic programming really stuck with me and when I had the opportunity to write a new version of this book, I took that opportunity to include this chapter on generics.
What we will learn in this chapter:
- What generics are
- How to create generic functions
- How to create generic types
- How to use Generic subscripts
- How to implement Copy-on-write
- How to design very flexible and reusable types with protocols and generics
Generics allow us to write very flexible and reusable code that avoids duplication. With a type-safe language, such as Swift...