Skip to content

Implement Eq alongside Hash #123

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

Merged
merged 1 commit into from
Jun 25, 2019
Merged

Implement Eq alongside Hash #123

merged 1 commit into from
Jun 25, 2019

Conversation

benesch
Copy link
Contributor

@benesch benesch commented Jun 25, 2019

It turns out implementing Hash alone is not very useful, as
std::collection::HashMap keys are required to implement both Hash and
Eq.

@benesch benesch requested a review from nickolay June 25, 2019 17:32
@nickolay
Copy link
Contributor

Would you mind commenting on why Eq makes sense for AST types? Otherwise I’ll get to it later - i’d have to reread the docs before I can say I understand this change.

Not for this PR: have you looked into whether it’s possible to share a common set of #[...] attributes across all of our AST types?

@coveralls
Copy link

coveralls commented Jun 25, 2019

Pull Request Test Coverage Report for Build 384

  • 40 of 40 (100.0%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 92.239%

Totals Coverage Status
Change from base Build 383: 0.0%
Covered Lines: 4326
Relevant Lines: 4690

💛 - Coveralls

It turns out implementing Hash alone is not very useful, as
std::collection::HashMap keys are required to implement both Hash and
Eq.

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
@benesch
Copy link
Contributor Author

benesch commented Jun 25, 2019

Would you mind commenting on why Eq makes sense for AST types? Otherwise I’ll get to it later - i’d have to reread the docs before I can say I understand this change.

You bet! The difference between PartialEq and Eq is that PartialEq does not require reflexivity (a == a for all a), while Eq does. The canonical example is that floats cannot implement Eq because NaN is not equal to itself. (Aside: this isn't an issue for us since we're already using OrderedFloat to provide Hash/Eq/PartialEq/Cmp/PartialCmd on our Value::Float variant.) There's essentially no situation where you don't want Eq if it makes sense, since otherwise you can't use the type as a key in a hash map.

Copy link
Contributor

@nickolay nickolay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right! If only it was called ReflexiveEq, I wouldn’t have so much trouble remembering it. Thanks, this makes perfect sense now.

@benesch
Copy link
Contributor Author

benesch commented Jun 25, 2019

Awesome, thanks for the review!

@benesch benesch merged commit cdba436 into apache:master Jun 25, 2019
@benesch benesch deleted the eq branch June 25, 2019 21:45
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

Successfully merging this pull request may close these issues.

4 participants