[ https://issues.apache.org/jira/browse/THRIFT-162?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12640651#action_12640651 ]
David Reiss commented on THRIFT-162: ------------------------------------ I agree that it is an interesting problem, but I think disallowing non-base types in sets and map keys is overkill. C++ enforces the immutability of set members and map keys (as best as it can, given its explicit pointers and direct access to memory) using const. As far as I can tell, Java's convention is to trust the application developer not to mutate keys rather than to make mutable objects unhashable. In Erlang, (almost) all values are immutable/copy-on-write, so there is no problem using mutable objects as keys. The best thing I can think of would be to put some verification code in each generator to make sure you aren't trying to do something that isn't appropriate for the language you are generating. > Thrift structures are unhashable, preventing them from being used as set > elements > --------------------------------------------------------------------------------- > > Key: THRIFT-162 > URL: https://issues.apache.org/jira/browse/THRIFT-162 > Project: Thrift > Issue Type: Bug > Components: Compiler (Python), Library (Python) > Reporter: Esteve Fernandez > Priority: Minor > Attachments: thrift_py_hash.patch > > > Let Foo be a Thrift structure: > struct Foo { > 1: i32 bar > } > If you want to use it properly as a set element or a as a dictionary key, the > autoegenerated Python code will complain about not being hashable: > >>> f1 = Foo() > >>> f1.bar = 1 > >>> f2 = Foo() > >>> f2.bar = 1 > >>> f1 == f2 > True > >>> set([f1]) & set([f2]) > set([]) > >>> d = {} > >>> d[f1] = 2 > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: unhashable instance > Since Thrift structures already implement __eq__ and __ne__, they should > implement __hash__ as well. The attached patch tries to mimic the behaviour > of the Java compiler, including a HashCodeBuilder class written in Python. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.