-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
make Timestamp implement IComparable (fixes #4267) #4318
Merged
anandolee
merged 1 commit into
protocolbuffers:master
from
warrenfalk:timestamp-comparison-operators
Jul 9, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a.Equals(b): Maybe I missed somewhere but didn't see Equals() implementation in this file. Shouldn't use a.CompareTo(b)==0 instead? Maybe add a test to compare two equal timestamps which are not normalized
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I think normalization at this level is a mistake and recommend reconsidering removing it. Let me answer your questions and explain my reasoning, and you can decide what you want to do.
Equals()
is already implemented in the other, generated half of thepartial
class. I can't change the implementation there, and I don't think we want to because ultimatelyTimestamp
is a protocol buffer structure and it should follow protocol buffer semantics where "equals" means "structurally all fields are equal".So if a user wants "time equality" instead of just "protocol buffer" equality then the user must (already, today) ensure that her structures are all normalized.
Keep in mind that equality and comparison are semantically separate things, which is why
IEquatable
andIComparable
are separate interfaces. "Comparison" deals with ordering/sorting. SoCompareTo(a) == 0
does not mean the same thing asEquals(a) == true
and they do not necessarily have to match.Additionally the operators don't match each other either. The
< > <= >=
operators are not necessarily related to==
. So it cannot be asserted that!(a < b) && !(a > b) == (a == b)
.But of course people expect them to match, so I think we should make them match.
If you want them to match, however, you have to give up on normalization because Equals ignores it and can't be changed. So if you want "time comparison" then you have to pre-normalize, just like you have to already pre-normalize if you want "time equality".
So the bottom line is you can either have comparisons that normalize, or comparisons that match equality, but not both. You have to choose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, we should make them match. I talked with our tech lead, we think it make sense to not normalize for all comparison. Add comments that if the timestamp is not normalized the result may not correct in the API documentation