Protocol-Oriented Library Design: Part 2

Episode #38 • Nov 19, 2018 • Subscriber-Only

With our library fully generalized using protocols, we show off the flexibility of our abstraction by adding new conformances and functionality. In fleshing out our library we find out why protocols may not be the right tool for the job.

Protocol-Oriented Library Design: Part 2
Introduction
00:05
Making strings diffable and snapshottable
00:53
Writing a text-based snapshot test
03:55
A missing protocol requirement
04:36
Ergonomical error messages
07:43
What’s the point?
11:30

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Introduction

Ok! So everything compiles and tests run and pass just as before! Unfortunately it was pretty complicated. We really butted heads with some seriously complicated protocol features, like Self, non-final classes, and required initializers. But at least now the Snapshottable protocol is completely decoupled from taking image snapshots. In fact, it’s more generic than any snapshot testing library out there. Can we do anything interesting with that?

This episode is for subscribers only.

Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

Already a subscriber? Log in

Exercises

  1. Using our series on protocol witnesses (part 1, part 2, part 3, part 4) as a guide, translate the Diffable protocol into a Diffing struct.

  2. Translate the Snapshottable protocol into a Snapshotting struct. How do you capture the associated type constraint?

  3. Translate each conformance of Diffable into a witness value on Diffing.

    • String
    • UIImage
  4. Translate the Snapshottable protocol into a Snapshotting struct. How do you capture the associated type constraint?

  5. Translate each conformance of Snapshottable into a witness value on Snapshotting.

    • String
    • UIImage
    • CALayer
    • UIView
    • UIViewController
  6. Translate the assertSnapshot generic algorithm to take an explicit Snapshotting witness.

References

Protocol-Oriented Programming in Swift

Apple • Tuesday Jun 16, 2015

Apple’s eponymous WWDC talk on protocol-oriented programming:

At the heart of Swift’s design are two incredibly powerful ideas: protocol-oriented programming and first class value semantics. Each of these concepts benefit predictability, performance, and productivity, but together they can change the way we think about programming. Find out how you can apply these ideas to improve the code you write.

uber/ios-snapshot-test-case

Uber, previously Facebook

Facebook released a snapshot testing framework known as FBSnapshotTestCase back in 2013, and many in the iOS community adopted it. The library gives you an API to assert snapshots of UIView’s that will take a screenshot of your UI and compare it against a reference image in your repo. If a single pixel is off it will fail the test. Since then Facebook has stopped maintaining it and transfered ownership to Uber.

Snapshot Testing in Swift

Stephen Celis • Friday Sep 1, 2017

Stephen gave an overview of snapshot testing, its benefits, and how one may snapshot Swift data types, walking through a minimal implementation.

Protocol Witnesses: App Builders 2019

Brandon Williams • Friday May 3, 2019

Brandon gave a talk about “protocol witnesses” at the 2019 App Builders conference. The basics of scraping protocols is covered as well as some interesting examples of where this technique really shines when applied to snapshot testing and animations.

Protocol-oriented programming is strongly recommended in the Swift community, and Apple has given a lot of guidance on how to use it in your everyday code. However, there has not been a lot of attention on when it is not appropriate, and what to do in that case. We will explore this idea, and show that there is a completely straightforward and mechanical way to translate any protocol into a concrete datatype. Once you do this you can still write your code much like you would with protocols, but all of the complexity inherit in protocols go away. Even more amazing, a new type of composition appears that is difficult to see when dealing with only protocols. We will also demo a real life, open source library that was originally written in the protocol-oriented way, but after running into many problems with the protocols, it was rewritten entirely in this witness-oriented way. The outcome was really surprising, and really powerful.