Swift: Equatable with Optionals

@andrewcbancroft recently wrote a great article about how Every Swift Value Type Should Be Equatable – go ahead and read it, I’ll wait…

One thing that stood out as missing from this article was comparing different sets of optional values. In the past, I have implemented the following equality for values to make sure optionals were covered:

I mentioned this in my Swift Newsletter this week, only to get the following responses on Twitter:

Of course, I had to see it for myself, so I tried this in the playground:

Turns out, the Equatable implementation for optionals is not necessary. It just works!

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.