My understand is that Equatable can be checked with ==
. And Identifiable can not be checked with ===
, but references can.
Is this correct? Would love pointers to discussions / explanations of this topic.
My understand is that Equatable can be checked with ==
. And Identifiable can not be checked with ===
, but references can.
Is this correct? Would love pointers to discussions / explanations of this topic.
=== effectively checks if two reference types refer to the same memory address - IE are the exact same instance.
Two instances of the same class could have all properties set to the same values, and therefore be equivalent according to Equatable and the == operator. That does not mean they share their ObjectIdentifier I.E. memory address. They’re separate instances.
Identifiable defines that the conforming type has an id and that the ID is hashable. That’s it. Commonly used to express “things that can be identified by id”.
Having the same ID does not mean two instances are Equivalent. They could be two users with the same ID, but a different profile in the case of a database entry changing for example.
The reason I'm curious is because I find that ==
is natural to write. And ===
feels like a version in the same family. But $0.id == $1.id
has a different shape, yet is what I should probably be using most of the time.
I was wondering if this is something that has been discussed (and possibly beaten to death ) already.
These are three distinct concepts useful in different scenarios.
Equatable
defines ==
check and allows to compare objects based on their contents, to put in other words — if two boxes has same contents, still being to different boxes. This is something you can define on your types and control what for this type is meant to be equal.===
is built-in check if this is the same instance (same box you are looking at), so it checks that reference in memory to it is the same.Identifiable
allows you to just define some entity that allows to identify object (some label on the box describing it). Objects with same identity can be different in its contents. This for example is useful when you want to track object with the same identity while its contents changes.