Back to Basics: Hashable

Episode #298 • Oct 14, 2024 • Subscriber-Only

While the documentation for Equatable discusses the notions of “equivalence relation” and “substitutability”, there are conformances in the Standard Library that run afoul, but for pragmatic reasons. Let’s explore them and then dive deeper into a related protocol: Hashable.

Collection
Equatable and Hashable
Back to Basics: Hashable
Locked

Unlock This Episode

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

Sign in with GitHub

Introduction

Brandon

So, that was a really deep dive into the Equatable protocol. I bet most of our viewers didn’t expect to get dragged into a abstract mathematical hole in order to understand something that seems to be so simple.

And here is where we try to bring things down to Earth a bit.

Everything we have said up to this point is technically correct, and as you can see there are a lot of weird things that can happen if you start to indiscriminately fudge with the concept of equality. You can very easily write reasonably looking code that gives very unreasonable results, and that completely breaks out intuition when it comes to understanding how code is supposed to work by reading it.

Stephen

However, even in the standard library there are multiple examples of Equatable implementations that do not live up to the promise of the documentation of the protocol. And sometimes we need to strike a balance between an idealized mathematical world, and the real, pragmatic world.

So, let’s take a few looks at how these ideas can fall apart in practice, and why we have chosen to live with it.

Pragmatism and Equatable


References

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing