Understanding parametric polymorphism and generic code
Let's imagine we want to organize a party for specific animals. We don't want to mix cats with dogs because the party would end up with the dogs chasing cats. We want a party, and we don't want intruders. However, at the same time, we want to take advantage of the procedures we create to organize the party and replicate them with frogs in another party; it would be a party of frogs. We want to reuse the procedures for either dogs or frogs. However, in future, we will probably want to use them with other animals, such as parrots, lions, tigers, and horses.
In the previous chapter, you learned how to work with protocols. We can declare a protocol to specify the requirements for an animal and then take advantage of Swift features to write a generic code that works with any class that implements the protocol. Parametric polymorphism allows us to write generic and reusable code that can work with values without depending on the type, while...