-
Notifications
You must be signed in to change notification settings - Fork 3
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
Cannot compare on PType #283
Comments
Can we safely override hashcode? |
@CThuleHansen I don't know the background to this one, but if you change the hashCode for a type, you have to be sure that the various Java contracts are honoured, otherwise you get some very peculiar errors. For example, hashCode and equals must be consistent; you also have to be careful about compareTo (especially if that uses equals). If you get this wrong, then complex aggregates of these types (typically maps) start to produce unexpected results. |
@nickbattle thank you very much for giving advice!
This is ASTCreator stuff, anything you are familiar with? Kenneth is on vacation :) |
What sort of Map are you using? It makes a difference: if you're using TreeMaps, then the compareTo method contract must be respected; if you're using HashMap, then the equals/hashCode contract must be respected (though it's generally a good idea to respect EVERY contract!). |
(The problem in VDMJ was that the compareTo method of Values is effectively user-defined, if you include an "ord" clause on a type. But that meant that you could write VDM that causes Java's map library to fail to find entries in the map!) |
@nickbattle |
The HashMap uses the hash of the key to locate a bucket, and then uses equals to find the item in that bucket. But, depending on the hash, you might end up putting an item in the wrong bucket and then it cannot locate it again when "the same" key is used later (ie. similar, but not "the same", like a Long or an Integer of the same value might have different hashes?). |
If the equals is using toString, the hashCode probably ought to use that too (ie. return toString().hashCode()) |
I agree with @nickbattle not sure why this is not the case. Like the mismatch here can lead to pretty strange behaviour so will a fix. |
So it seems like we should make a utility method for this... |
PType is used as key in HashMap in several places in the
fmi2api
project, i.e.ioBuffer
,sharedBuffer
.However,
PType
implementers only overridesequals
and nothashCode
. Therefore, it does not work as regular keys inHashMap
and requires additional functions such as https://github.com/INTO-CPS-Association/maestro/blob/development/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/ComponentVariableFmi2Api.java#L127-L137For example, the groupingBy collector does not allow for comparison on PType.
prints
{real=1, real=1}
but the ideal would be
{real=2}
such that they are grouped together.hashcode
for thePType
implementors?TreeMap
instead ofHashMap
? (worse in terms of performance...)The text was updated successfully, but these errors were encountered: