This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!See subscription optionsorLog in
Sign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!Sign up for free episode
In the last two weeks (episodes #33 and #34) we explored the idea that protocols could largely be translated into concrete datatypes, and in doing so we fixed some of the problems that protocols have, but more importantly, we opened up the doors to some amazing compositions that were completely hidden from us with protocols. Towards the end of those two episodes we claimed that knowing this correspondence between protocols and concrete datatypes could actually lead us to better API and library design, but stopped short on the details.
Well, we want to describe that, but before we can get there we need to go a little deeper with this correspondence. Currently our dictionary to translate between the world of protocols and the world of concrete types is woefully incomplete. There are tons of protocol concepts that we haven’t yet described how to translate over to concrete types, and knowing that information will provide us the tools we need to tackle API and library design. So, that is this week’s goal!
But first, we want to introduce a lil clean up terminology that we’ve been using on the series…
Currently in Swift (as of 4.2) there is no way to extend tuples to conform to protocols. Tuples are what is
known as “non-nominal”, which means they behave differently from the types that you can define. For example, one
cannot make tuples
Equatable by implementing
extension (A, B): Equatable where A: Equatable, B: Equatable.
To get around this Swift implements overloads of
== for tuples, but they aren’t truly equatable, i.e.
you cannot pass a tuple of equatable values to a function wanting an equatable value.
However, protocol witnesses have no such problem! Demonstrate this by implementing the function
pair: (Combining<A>, Combining<B>) -> Combining<(A, B)>. This
function allows you to construct a combining witness for a tuple given two combining witnesses for each
component of the tuple.
Functions in Swift are also “non-nominal” types, which means you cannot extend them to conform to protocols.
However, again, protocol witnesses have no such problem! Demonstrate this by implementing the function
pointwise: (Combining<B>) -> Combining<(A) -> B>. This allows you to construct
a combining witness for a function given a combining witnesss for the type you are mapping into. There
is exactly one way to implement this function.
Protocols in Swift can have “associated types”, which are types specified in the body of a protocol but aren’t determined until a type conforms to the protocol. How does this translate to an explicit datatype to represent the protocol?
RawRepresentable protocol into an explicit datatype
struct RawRepresenting. You will
need to use the previous exercise to do this.
Protocols can inherit from other protocols, for example the
Comparable protocol inherits from the
Equatable protocol. How does this translate to an explicit datatype
to represent the protocol?
Comparable protocol into an explicit datatype
struct Comparing. You will need to use the
previous exercise to do this.
An old article detailing many of the pitfalls of Swift protocols, and how often you can simplify your code by just using concrete datatypes and values. Chris walks the reader through an example of some networking API library code, and shows how abstracting the library with protocols does not give us any tangible benefits, but does increase the complexity of the code.
Matt gives another account of protocol-oriented programming gone awry, this time by breaking down the famous WWDC talk where a shape library is designed using protocols. By rewriting the library without protocols Matt ends up with something that can be tested without mocks, can be inspected at runtime, and is more flexible in general.
Haskell’s notion of protocols are called “type classes,” and the designers of Swift have often stated that Swift’s protocols took a lot of inspiration from Haskell. This means that Haskellers run into a lot of the same problems we do when writing abstractions with type classes. In this article Gabriel Gonzalez lays down the case for scrapping type classes and just using simple datatypes.
A few months after releasing our episode on Contravariance we decided to rename this fundamental operation. The new name is more friendly, has a long history in mathematics, and provides some nice intuitions when dealing with such a counterintuitive idea.
This article describes the ideas of contravariance using the Haskell language. In many ways exploring functional programming concepts in Haskell is “easier” because the syntax is sparse and allows you to focus on just the core ideas.