Swift: Protocol Composition 😍

I’ve been incorporating Protocol-Oriented Programming more and more in my Swift code and I LOVE it!!! Thanks again to @mhollemans for the article on Mixins and Traits in Swift 2.0 – that really helped me understand how to use protocols in a very powerful and beautiful way.

However, I’ve found that in practice, my objects sometimes conform to too many protocols, so the method or classes that take in these protocols become way too long and a bit confusing, just like in the below configure method:

So one solution to this that I really like was suggested to me by @zachwaugh: Protocol Composition!!! It looks like this:

I seriously love this 🤓😍. It is readable, avoids generics, and communicates the intention of the protocols very clearly. Thanks again @zachwaugh for suggesting it!

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.