POP Art: Understanding Protocol-Oriented Programming
Protocol-Oriented Programming seems like something you can quickly dismiss, but there's a lot going on beneath the surface.
Join the DZone community and get the full member experience.Join For Free
So, last year when all the excitement was frothing about this Protocol-Oriented Programming “revolution,” First Protocol-Oriented Language and all — did you take a quick look and chuckle to yourself , thinking “Ah, they’ve rediscovered Mixin-Based Programming, isn’t that cute?” and move on? Yeah, us too. But it turns out there is a good bit more depth to plumb! Here’s an excellent tutorial style introduction, released as a teaser for the RWDevCon 2016 Vault video collection:
Here are 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 modeling
- 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:
And here is an absolutely essential series on associated types from Russ Bishop:
Sometimes I think type theory is deliberately obtuse and all those functional programming hipster kids are just bull-shitting as if they understood one word of it…
I don’t feel like I fully covered one aspect of protocols with associated types: why can they be such a pain to work with?
So the philosophical answer to the question of Why Associated Types? is that the Swift core team believes they are a better way to model concepts. They don’t have the problem of multiple conformances, they encapsulate the details of a concept cleanly, and they’re less fragile.
And there you have it. By way of example, here’s a few practical applications of protocols to make common tasks more elegant:
And a wide-ranging selection of other inside baseball style musings on generics and protocols to help you grok the gestalt here:
Plenty there to keep you pondering for a while we trust!
Published at DZone with permission of Alex Curylo, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.