RWDevCon 2016 Session 303: Introduction to Protocol-Oriented Programming

Alexis Gallagher

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:

POP-Venn.001

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

    • you can do almost everything you can with inheritance
    • and, they can work with value types
    • and, they allow retroactive modelling
  • but, protocols with associated types (PATs) are different beasts

    • 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:

POP-comparison.001

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!
Alexis Gallagher

Alexis Gallagher has been failing to tear himself away from computers for over a dozen years, including tours in e-commerce firms in New York, in scientific research in Oxford, in front-office trading systems in London, and now via iOS in a pocket near you.

Other Items of Interest

Big Book SaleAll raywenderlich.com iOS 11 books on sale for a limited time!

raywenderlich.com Weekly

Sign up to receive the latest tutorials from raywenderlich.com each week, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

PragmaConf 2016 Come check out Alt U

Our Books

Our Team

Video Team

... 19 total!

iOS Team

... 71 total!

Android Team

... 17 total!

Unity Team

... 11 total!

Articles Team

... 15 total!

Resident Authors Team

... 18 total!

Podcast Team

... 7 total!

Recruitment Team

... 9 total!