RWDevCon 2016 Session 303: Introduction to Protocol-Oriented Programming

Swift 2 protocols can do things impossible in Objective C, so much that Apple has even argued for “Protocol-Oriented Programming” as an alternative to object-oriented programming. What does this really mean and when is it wise? By Alexis Gallagher.

Leave a rating/review
Save for later


Hide contents

RWDevCon 2016 Session 303: Introduction to Protocol-Oriented Programming

5 mins

We recently released the RWDevCon 2016 Vault, a collection of over 24+ hours of hands-on video tutorials, 700+ MB of sample projects, and a 570 page conference book.

To celebrate its launch, we’re releasing 3 videos from the vault for free so you can check them out and see if you like them.

Today’s video is an example from the advanced track – Introduction to Protocol-Oriented Programming. Enjoy!

Note: This video is free! But there is a bug in MobileSafari iOS 9.3.1 which may block playback and show a warning about privacy settings. To workaround this, just watch it from a desktop browser or from Google Chrome for iOS.


My mind was blown in the best possible way by @DaveAbrahams’s WWDC 2015 talk on Protocol-Oriented Programming, or POP.

But frankly it took me a while to digest it. That talk covers a lot of ground — not just ordinary protocols, but protocols with associated types, protocol extensions, the advantages of value types, and a general argument for using protocols instead of inheritance. So I watched the talk, then I watched it again (and again).

Protocols with Associated Types: a New Thing

Part of the key for me was realizing that protocols with associates types — or PATs, as I like to call them — are quite different from ordinary protocols. I think to understand their strengths and their limitations it’s essential to understand what problem they solve that ordinary protocols cannot solve, and to understand how they resemble features in other languages like C++, Haskell, etc..

After mulling this, I ended up giving a talk on it at last year’s Functional Swift Conference: Protocols with Associated Types and How They Got That Way.

I’d say the top-line summary is:

  • PATs are not esoteric. They are everywhere, since even Equatable is a PAT. So you need to use them and understand them.
  • To use PATs, you must use generics, since PATs can only be used with generics.
  • When you use generics, you lose dynamic dispatch, which unfortunately may be why you wanted a traditional protocol to begin with.
  • To keep using dynamic dispatch, you need to do an awkward, boilerplatey backflip called type erasure, or find some other solution. But maybe the language will fix this later.

Why is this puzzling? Here is a helpful Venn Diagram showing the overlap between PATs and traditional protocols:


Punchline: there is no overlap!

Protocols with Protocol Extension: a Replacement for Inheritance

But, having now come to grips with PATs, that still leaves all the rest of Dave’s awesome talk to reckon with. PATs were just one aspect of it. Most of the talk is arguing for protocol-oriented solutions over inheritance-based solutions.

This RWDevCon tutorial talk above is my attempt to cover much of the rest of Dave’s talk, and walk through it in a way that is completely concrete and systematic. It compares inheritance, vs protocols with protocol extensions, vs protocols with associated type, and spells out the pros/cons along the way. (It is a methodical march, which I hope covers the terrain thoroughly.)

Okay, okay, okay what’s the summary? Here’s the key points:

protocols with extensions are almost perfectly superior to inheritance

but, protocols with associated types (PATs) are different beasts

    • you can do almost everything you can with inheritance
    • and, they can work with value types
    • and, they allow retroactive modelling
    • you give up dynamic dispatch
    • but you can model more intricate type relations
  • you can do almost everything you can with inheritance
  • and, they can work with value types
  • and, they allow retroactive modelling
  • you give up dynamic dispatch
  • but you can model more intricate type relations

Or if you just want one nugget to mull over, I’d say most of it is in this slide:


The video above is not a normal conference tech talk, but a tutorial talk, a format which as far as I know is unique to RWDevCon. Everyone in the room works through the material at the same time.

If you want to follow along with the video, or review the material by working through an extended Swift playground, you can Download demo resources.

Note from Ray: Did you enjoy this tutorial? If so, sign up to our RWDevCon newsletter to be notified when RWDevCon 2017 tickets go on sale, or check out our massive vault of tutorial videos!


Over 300 content creators. Join our team.