How to determine if two mutable data structures are the same?
This way of comparison is not directly exposed in top-level at the moment.
However, it could be done through the compiler, for example
sameInstanceQ =
FunctionCompile[Function[{Typed[e1, "Expression"], Typed[e2, "Expression"]},
Native`SameInstanceQ[e1, e2]]];
a = CreateDataStructure["Value", 1];
b = a["Copy"];
c = b;
SameQ[a, b, c]
(* True *)
sameInstanceQ[a, b]
(* False *)
sameInstanceQ[b, c]
(* True *)
A new entry in the function repository, SameInstanceQ
, can also be used here:
In[31]:= a = CreateDataStructure["Value", 1];
b = a["Copy"];
c = b;
In[34]:= ResourceFunction["SameInstanceQ"][a, b]
Out[34]= False
In[36]:= ResourceFunction["SameInstanceQ"][b, c]
Out[36]= True
It can also work with normal Wolfram Language expressions and is not limited to data structures:
In[42]:= a = <|"a" -> 1|>;
b = <|"a" -> 1|>;
c = b;
In[45]:= ResourceFunction["SameInstanceQ"][a, b]
Out[45]= False
In[46]:= ResourceFunction["SameInstanceQ"][b, c]
Out[46]= True
This function exposes some internal implementation details that are interesting, like the fact that there is only one version of an empty association:
In[47]:= ResourceFunction["SameInstanceQ"][<||>, <||>]
Out[47]= True
Other corner cases are discussed on the ref page.