A new Swift video series exploring functional programming and more.
#38 • Monday Nov 19, 2018 • Subscriber-only

Protocol-Oriented Library Design: Part 2

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.

#38 • Monday Nov 19, 2018 • Subscriber-only

Protocol-Oriented Library Design: Part 2

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.


Subscribe to Point‑Free

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


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.

Chapters
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
Credits

Hosted by Brandon Williams and Stephen Celis. Recorded in Brooklyn, NY.