-
Notifications
You must be signed in to change notification settings - Fork 397
Add round-tripping Equality/IComparable contract generation #838
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
Changes from all commits
14559d5
28d55bc
7b15a7f
a9dd986
67bb63a
b2ba83f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -668,25 +668,25 @@ private void GenerateEqualityAndComparison() | |
/// <summary>Returns true if less or equal to.</summary> | ||
public static bool operator <=({_quantity.Name} left, {_quantity.Name} right) | ||
{{ | ||
return left.Value <= right.GetValueAs(left.Unit); | ||
return left.CompareTo(right) <= 0; | ||
}} | ||
|
||
/// <summary>Returns true if greater than or equal to.</summary> | ||
public static bool operator >=({_quantity.Name} left, {_quantity.Name} right) | ||
{{ | ||
return left.Value >= right.GetValueAs(left.Unit); | ||
return left.CompareTo(right) >= 0; | ||
}} | ||
|
||
/// <summary>Returns true if less than.</summary> | ||
public static bool operator <({_quantity.Name} left, {_quantity.Name} right) | ||
{{ | ||
return left.Value < right.GetValueAs(left.Unit); | ||
return left.CompareTo(right) == -1; | ||
}} | ||
|
||
/// <summary>Returns true if greater than.</summary> | ||
public static bool operator >({_quantity.Name} left, {_quantity.Name} right) | ||
{{ | ||
return left.Value > right.GetValueAs(left.Unit); | ||
return left.CompareTo(right) == 1; | ||
}} | ||
|
||
/// <summary>Returns true if exactly equal.</summary> | ||
|
@@ -715,7 +715,9 @@ public int CompareTo(object obj) | |
/// <inheritdoc /> | ||
public int CompareTo({_quantity.Name} other) | ||
{{ | ||
return _value.CompareTo(other.GetValueAs(this.Unit)); | ||
var asFirstUnit = other.GetValueAs(this.Unit); | ||
var asSecondUnit = GetValueAs(other.Unit); | ||
return (_value.CompareTo(asFirstUnit) - other.Value.CompareTo(asSecondUnit)) / 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully grok this one. I get that we are doing an average of comparing A and B in both A's unit and in B's unit. What exactly does this solve? Is it when A's and B's units are really different and trying to represent either quantity in the other's unit results in big rounding errors? Why does averaging help? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure- so basically the HashCode is used in some collections for fast lookup of objects. It is part of the 'Equality Contract' - which states that 'Equals(x,y) => Equals(x.GetHashCode(), y.GetHashCode()))', yet at the same time the result of GetHashCode is not considered as uniquely identifying (it is possible to have the same HashCode for two objects for which Equals(x,y) == false). When creating hashing functions one tries to make the result as unique as possible- but not such that it violates the first condition- typically we take the result of combining the hashes of the properties that make up the Equals function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why we wouldn't want this compared to raw double comparison in equivalent units. |
||
}} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -792,7 +794,7 @@ public bool Equals({_quantity.Name} other, double tolerance, ComparisonType comp | |
/// <returns>A hash code for the current {_quantity.Name}.</returns> | ||
public override int GetHashCode() | ||
{{ | ||
return new {{ QuantityType, Value, Unit }}.GetHashCode(); | ||
return QuantityType.GetHashCode(); | ||
}} | ||
|
||
#endregion | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I like this notation better