Swift is a modern language and, coming from a purely object-oriented language background, can feel overwhelming. With its strong functional capabilities as well as extensive generics support, Swift is an expressive language that generalizes an interesting programming paradigm: protocol-oriented programming.
Protocol-oriented programming doesn't replace object-oriented programming or functional programming—it's another tool that helps you elevate your code bases to new heights. It doesn't magically transform poorly written code into unicorns and rainbows.
Alongside powerful protocols come generics-oriented programming. Generics let you write functions with protocols instead of concrete types. When programming with concrete types, only children of this type, through inheritance, can be used in place of the parent type. With protocols and generics, any type that conforms to, or adopts, the protocol can be used. This leads to a slew of unprecedented capabilities, such as building algorithms that are independent of the data they work with.
In this chapter, we will do the following:
- Define what protocol-oriented programming is, where it comes from, and in which cases you should use it
- Explore how to apply the template pattern through protocol-oriented programming
- Generalize what we've uncovered on generics and protocols
- Learn how to implement effective type erasure and when those techniques may help you in your own code base