Repository navigation
Description
Input objects that implement IEquatable<T> but not also IComparable<T> aren't properly compared in terms of equality.
The root cause is that a comparison method is used that requires not just an equality test, but one for ordering values: if the .Equals() call in .TryCompareValue() indicates false, the result isn't used (which is appropriate for Object.Equals(), but not for IEquatable<T>.Equals()) - instead, the code falls back to .ToString() comparison, which for types that don't override this method reports the same value - the full type name - for all instances, causing all instances to compare as equal.
See
| public static bool TryCompare(object first, object second, bool ignoreCase, out int result) |
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ObjectCommandComparer.cs
Line 222 in 16ec1cc
| string firstString = PSObject.AsPSObject(first).ToString(); |
Related:
Steps to reproduce
# Define class Foo that wraps an integer and implements IEquatable<T>
# to return true if two instances wrap the same integer value.
Add-Type 'public class Foo : System.IEquatable<Foo> { public int Bar = 0; public bool Equals(Foo other) { return Bar == other.Bar; } public override bool Equals(object o) { return o is Foo && Equals((Foo) o); } public override int GetHashCode() { return Bar; } }'
$o1 = [Foo]::new(); $o1.Bar = 1
$o2 = [Foo]::new(); $o2.Bar = 2
# OK
$o1.Equals($o2) | Should -BeFalse
# FAILS, because $o1 and $o2 are unexpectedly considered equal.
Compare-Object $o1 $o2 | Should -Not -BeNullOrEmptyExpected behavior
Both tests should succeed.
Actual behavior
The 2nd test fails, because $o1 and $o2 are unexpectedly considered equal, so Compare-Object produces no output.
Expected a value, but got $null or empty.
Environment data
PowerShell Core 7.1.0-preview.2
Activity
iSazonov commentedon Apr 28, 2020
"Ordering" is a fundamental nature of the cmdlet. I wonder why you ask about equality for it.
Also if you implement a class a best practice is to implement ToString() method.
mklement0 commentedon Apr 28, 2020
The cmdlet offers you to determine equal and different objects - where do you see ordering (except in the implementation)?
Even if you do override
.ToString(), signaling equality via instances' string representations is not the appropriate mechanism - that is whatIComparableand / orIEquatableare for.iSazonov commentedon Apr 28, 2020
:-) Sorry, my first thoughts was about output with "=>".
iSazonov commentedon Apr 28, 2020
Windows PowerShell has the same behavior. Fixing
TryCompare()would be a breaking change and not only in the scenario. There is aCompare()method too with the same behavior. I believe the design was to implement exactlycompare, notequalityin the methods and throw if objects are not comparable. I believe we could create Equals() methods in the call tree and use it in Compare-Object cmdlet.microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.