Chapter 8. Putting the pro in protocol-oriented programming

This chapter covers

  • The relationship and trade-offs between generics and using protocols as types
  • Understanding associated types
  • Passing around protocols with associated types
  • Storing and constraining protocols with associated types
  • Simplifying your API with protocol inheritance

Protocols bring a lot of power and flexibility to your code. Some might say it’s Swift’s flagship feature, especially since Apple markets Swift as a protocol-oriented-programming language. But as lovely as protocols are, they can become difficult fast. Plenty of subtleties are involved, such as using protocols at runtime or compile time, and constraining protocols with associated types.

This chapter’s ...

Get Swift in Depth now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.