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

Add equality operators for Period and ZoneInterval #1530

Merged
merged 2 commits into from
May 9, 2020

Conversation

jskeet
Copy link
Member

@jskeet jskeet commented May 8, 2020

Additionally, add a check to ensure we don't miss this again, at
least for anything implementing IEquatable<T>

Fixes #1529.

Copy link
Contributor

@malcolmr malcolmr left a comment

Choose a reason for hiding this comment

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

LGTM, optional suggestions below.


var failures = AllPublicTypes
.Where(t => typeof(IEquatable<>).MakeGenericType(t).IsAssignableFrom(t))
.Where(t => t.GetMethod("op_Equality") is null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Presumably reasonable to add a similar check here for op_Inequality as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

No need - the C# language won't let you overload == without overloading != too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hurrah for common sense! (Another option would have been to generate synthetic operators given == and <, but this is good too.)

.ToList();
Assert.IsEmpty(failures);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

At the risk of being somewhat pedantic, how about an additional test for IComparable types having all of {<, <=, >=, >} too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm. I think we're likely to need a lot more exceptions for this, as it's quite reasonable to make things comparable without overloading those operators (e.g. string) but I'll try adding the test anyway - if we only need a few exceptions, it wouldn't hurt to have a reminder.

@jskeet
Copy link
Member Author

jskeet commented May 9, 2020

It turns out this was worth doing - I've added operators for YearMonth. PTAL.
(I've also noticed a possible performance issue in comparisons for both YearMonth and LocalDate - will benchmark that now.)

@jskeet
Copy link
Member Author

jskeet commented May 9, 2020

Just realized I need to add tests for the operators in YearMonth - that should be a matter of changing which helper method we call though.

jskeet added 2 commits May 9, 2020 08:02
Additionally, add a check to ensure we don't miss this again, at
least for anything implementing `IEquatable<T>`

Fixes 1529.
Additionally, add a consistency check to avoid this problem happening again
@codecov-io
Copy link

Codecov Report

Merging #1530 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1530   +/-   ##
=======================================
  Coverage   99.23%   99.23%           
=======================================
  Files         153      153           
  Lines        6817     6829   +12     
=======================================
+ Hits         6765     6777   +12     
  Misses         52       52           
Impacted Files Coverage Δ
src/NodaTime/Period.cs 100.00% <100.00%> (ø)
src/NodaTime/TimeZones/ZoneInterval.cs 100.00% <100.00%> (ø)
src/NodaTime/YearMonth.cs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 46f8bba...556b306. Read the comment docs.

Copy link
Contributor

@malcolmr malcolmr left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!


var failures = AllPublicTypes
.Where(t => typeof(IEquatable<>).MakeGenericType(t).IsAssignableFrom(t))
.Where(t => t.GetMethod("op_Equality") is null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hurrah for common sense! (Another option would have been to generate synthetic operators given == and <, but this is good too.)

@jskeet jskeet merged commit cbaeabd into nodatime:master May 9, 2020
@jskeet jskeet deleted the equality branch May 9, 2020 10:15
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.

Period does not implement operator ==
3 participants