-
Notifications
You must be signed in to change notification settings - Fork 75
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
There should be a way to opt out of "Significant fields" #142
Comments
An important design goal of EqualsVerifier is to err on the side of caution, which is why this check is there. Maybe you're right, and it should be possible to suppress. Do you have a specific use case that caused you to file this issue, or is it more of a principle thing for you? |
It's not just a principle thing, I often write hashCode this way. I filed the issue in response to doing so on https://github.com/commercehub-oss/clouseau/blob/master/clouseau-api/src/main/java/com/commercehub/clouseau/api/Session.java; I had to abandon EqualsVerifier for this class. |
Causing you to abandon EqualsVerifier is obviously not a design goal :). Just to try to understand the situation better, though: is there a reason why only the sessionId can be used in hashCode(), and not the other fields that are used in equals()? You'll achieve much better spread if you include those other fields as well. Abandoning EqualsVerifier seems to imply it's a deal-breaker to add those fields to hashCode(), and I don't understand why that would be the case here. |
I think at some point in this code's history, that approach presented an issue. It seems viable now in this case. I'll give you some other conceptual cases:
|
Another case: (3.) Sometimes we have objects which have equality defined in terms of immutable identity properties and mutable value properties, and these objects get added to hash-based collections. Then either: You might be able to make an argument that hash-based collections should not have been used, but I've seen enough occurrences of these idioms to not want them excluded. Erring on the side of caution is great for defaults, but a suppression option would expand the potential applicability of EqualsVerifier. |
Hi Paul, Thanks for the elaborate reply, but you already had me by your first example :). I don't do a lot of legacy work these days so I didn't think of this scenario immediately. I think this is a very compelling use case, so I'll add a switch to the next version. Note that I have a couple other issues that I'm also working on, so it won't be done right away. I'll update this ticket when I release something. |
Thanks
|
I've added |
"Significant fields: equals relies on foo, but hashCode does not"
Satisfying this check might be considered a sane default (though I'm not even sure I agree with that). However, it is by no means a requirement for the implementation of the two methods to be compatible with each other and the spec. There should be a way to suppress this check.
Thanks for your consideration.
The text was updated successfully, but these errors were encountered: