Skip to content

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

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

}}

/// <summary>Returns true if exactly equal.</summary>
Expand Down Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

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

I don't fully grok this one.
I tried reading up on he last comments in #717, but I still don't quite see why this works and whether it is a good idea or if it has some edge-cases to it. Could you try to ELI5 ?

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
In practice all collections that use the hash function by definition compare both the result of the hash comparison, followed by a full Equals check- the first condition skipping 99.99% of the non-equal entities, while the second one ensures that we are not being tricked by a hash-collision.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 />
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException()
public void GetHashCode_Equals()
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Equal(new {{{_quantity.Name}.QuantityType, quantity.Value, quantity.Unit}}.GetHashCode(), quantity.GetHashCode());
Assert.Equal({_quantity.Name}.QuantityType.GetHashCode(), quantity.GetHashCode());
}}
");

Expand Down

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.

2 changes: 1 addition & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs

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.

2 changes: 1 addition & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs

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.

Loading