Protocol-Oriented Programming in Swift

Protocol-Oriented Programming in Swift

Swift – the First POP Language

At WWDC 2015, Apple announced that Swift is the world’s first Protocol-Oriented Programming (POP) language.

So What’s POP?

Protocol-Oriented Programming is a new programming paradigm ushered in by Swift 2.0. In the Protocol-Oriented approach, we start designing our system by defining protocols. We rely on new concepts: protocol extensions, protocol inheritance, and protocol compositions.

The paradigm also changes how we view semantics. In Swift, value types are preferred over classes. However, object-oriented concepts don’t work well with structs and enums: a struct cannot inherit from another struct, neither can an enum inherit from another enum. So inheritance – one of the fundamental object-oriented concepts – cannot be applied to value types.

On the other hand, value types can inherit from protocols, even multiple protocols. Thus, with POP, value types have become first class citizens in Swift.

Start with a Protocol

When designing a software system, we try to identify the elements needed to satisfy the requirements of a given system. We then model the relationships between these elements. We can start with a superclass and model its relationships through inheritance. Or we can start with a protocol and model the relationship as a protocol implementation. Swift provides full support for both interpretations. However, Apple tells us:

“Don’t start with a class, start with a protocol.”

Table of Contents

Continue reading this article on hack.guides() by Pluralsight

By | 2017-08-26T18:53:43+00:00 July 15th, 2017|iOS Programming, Pluralsight, Swift, Swift 3, Swift 3.0, Swift Programming|1 Comment

About the Author:

One Comment

  1. carlos July 23, 2017 at 2:12 pm - Reply

    Yay! My article is being featured on hack.guides() by Pluralsight

Leave A Comment