capnpy
capnpy copied to clipboard
Comparing structs with union fields
Following the "equality and hashing" guide: http://capnpy.readthedocs.io/en/latest/usage.html?highlight=new_#equality-and-hashing
I'd like to compare Structs containing Union fields for equality. I annotate the Struct with $Py.key("*"). But at runtime, the _key() method tries to get all the members of the union, which can never succeed.
$Py.key("*")
is a hack which I introduced for lazyness, but the more I think about it the more I am convinced it is a bad idea. To starts with, as explained in the docs, it easily breaks in presence of schema evolution. Moreover, it is hard(ish) to implement it in the following cases:
- anonymous union (as you discovered)
- groups: we would need some sort of "recursive _key" or something
- named unions, which have the problems of both
So, I am thinking about writing a generic schema-less "deep equality" algorithm which recursively compares two arbitrary pointers. This would be much more powerful than the current $Py.key("*")
because it will automatically solve all the problems above.
The only drawback I can think of is that we will loose compatibility with tuples: suppose we have the following schema:
struct Point $Py.key("*") {
x @0 :Int64;
y @1: Int64;
}
with the current behavior, it is guaranteed that Point(1, 2) == (1, 2)
and hash(Point(1, 2)) == hash((1, 2))
: if we switch to the schema-less recursive pointer equality, this will no longer be true for Py.key("*")
objects (but it will still be true if you list the fields explicitly, e.g. Py.key("x, y")
).
So: would this solve your problem? And, how urgent it is? :)