Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparing structs with union fields #7

Open
benmoran opened this issue Mar 9, 2017 · 1 comment
Open

Comparing structs with union fields #7

benmoran opened this issue Mar 9, 2017 · 1 comment

Comments

@benmoran
Copy link

benmoran commented Mar 9, 2017

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.

@antocuni
Copy link
Owner

$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? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants