So this proves that Swift Data has chosen to use referential identity for the Equatable
and Hashable
conformance of models, and we think this is the clearest sign that referential equality is typically the correct choice for classes. Even Swift Data models do not use structural equality, and they are classes that primarily represent simple data values held in some external store. So, if they can’t use structural equality, what hope do we have for other classes?
There’s another situation where referential equality is the only way to make a reference Equatable
, but it has to do with actors instead of classes. If you didn’t know, actors are reference types, but its stored properties are all protected by an isolation domain that requires you to await
to access and mutate the properties if you are in a different isolation domain.
Let’s take a look.