You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✨ Add back IEquatable with strict equality (#1100)
Fixes#1017
- Reverted removing `IEquatable`.
- Changed the implementation to strict equality on both `Value` and `Unit`.
- Improved tests.
- Improved xmldocs for equality members.
- `GetHashCode()` left unchanged, which includes quantity name in addition to value and unit, so that `LengthUnit.Meter = 1` and `MassUnit.Gram = 1` are still considered different in collections of `IQuantity`.
Reverts commit f3c7e25.
"🔥 Remove IEquatable<T> and equality operators/methods"
// We use obsolete attribute to communicate the preferred equality members to use.
756
+
// CS0809: Obsolete member 'memberA' overrides non-obsolete member 'memberB'.
757
+
#pragma warning disable CS0809
758
+
759
+
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
760
+
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
761
+
[Obsolete(""Consider using Equals(Angle, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
762
+
public static bool operator ==({_quantity.Name} left, {_quantity.Name} right)
763
+
{{
764
+
return left.Equals(right);
765
+
}}
766
+
767
+
/// <summary>Indicates strict inequality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
768
+
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
769
+
[Obsolete(""Consider using Equals(Angle, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
770
+
public static bool operator !=({_quantity.Name} left, {_quantity.Name} right)
771
+
{{
772
+
return !(left == right);
773
+
}}
774
+
775
+
/// <inheritdoc />
776
+
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
777
+
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
778
+
[Obsolete(""Consider using Equals(Angle, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
779
+
public override bool Equals(object obj)
780
+
{{
781
+
if (obj is null || !(obj is {_quantity.Name} otherQuantity))
782
+
return false;
783
+
784
+
return Equals(otherQuantity);
785
+
}}
786
+
755
787
/// <inheritdoc />
788
+
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
789
+
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
790
+
[Obsolete(""Consider using Equals(Angle, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
791
+
public bool Equals({_quantity.Name} other)
792
+
{{
793
+
return new {{ Value, Unit }}.Equals(new {{ other.Value, other.Unit }});
794
+
}}
795
+
796
+
#pragma warning restore CS0809
797
+
798
+
/// <summary>Compares the current <see cref=""{_quantity.Name}""/> with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit.</summary>
799
+
/// <param name=""obj"">An object to compare with this instance.</param>
/// <item><term> Less than zero</term><description> This instance precedes <paramref name=""obj"" /> in the sort order.</description></item>
807
+
/// <item><term> Zero</term><description> This instance occurs in the same position in the sort order as <paramref name=""obj"" />.</description></item>
808
+
/// <item><term> Greater than zero</term><description> This instance follows <paramref name=""obj"" /> in the sort order.</description></item>
809
+
/// </list>
810
+
/// </returns>
756
811
public int CompareTo(object obj)
757
812
{{
758
813
if (obj is null) throw new ArgumentNullException(nameof(obj));
759
-
if (!(obj is {_quantity.Name}obj{_quantity.Name})) throw new ArgumentException(""Expected type {_quantity.Name}."", nameof(obj));
814
+
if (!(obj is {_quantity.Name}otherQuantity)) throw new ArgumentException(""Expected type {_quantity.Name}."", nameof(obj));
760
815
761
-
return CompareTo(obj{_quantity.Name});
816
+
return CompareTo(otherQuantity);
762
817
}}
763
818
764
-
/// <inheritdoc />
819
+
/// <summary>Compares the current <see cref=""{_quantity.Name}""/> with another <see cref=""{_quantity.Name}""/> and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit.</summary>
820
+
/// <param name=""other"">A quantity to compare with this instance.</param>
821
+
/// <returns>A value that indicates the relative order of the quantities being compared. The return value has these meanings:
/// <item><term> Less than zero</term><description> This instance precedes <paramref name=""other"" /> in the sort order.</description></item>
825
+
/// <item><term> Zero</term><description> This instance occurs in the same position in the sort order as <paramref name=""other"" />.</description></item>
826
+
/// <item><term> Greater than zero</term><description> This instance follows <paramref name=""other"" /> in the sort order.</description></item>
0 commit comments