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.