Skip to content

Compare-Object doesn't respect IEquatable<T> #12498

Closed

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)
and
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 -BeNullOrEmpty

Expected 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

  1. added
    Issue-Question
    on Apr 26, 2020
  2. iSazonov commented on Apr 28, 2020

    @iSazonov
    Collaborator

    "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.

  3. mklement0 commented on Apr 28, 2020

    @mklement0
    ContributorAuthor

    "Ordering" is a fundamental nature of the cmdlet.

    The cmdlet offers you to determine equal and different objects - where do you see ordering (except in the implementation)?

    Also if you implement a class a best practice is to implement ToString() method.

    Even if you do override .ToString(), signaling equality via instances' string representations is not the appropriate mechanism - that is what IComparable and / or IEquatable are for.

  4. iSazonov commented on Apr 28, 2020

    @iSazonov
    Collaborator

    :-) Sorry, my first thoughts was about output with "=>".

  5. iSazonov commented on Apr 28, 2020

    @iSazonov
    Collaborator

    Windows PowerShell has the same behavior. Fixing TryCompare() would be a breaking change and not only in the scenario. There is a Compare() method too with the same behavior. I believe the design was to implement exactly compare, not equality in 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.

  6. added
    Issue-Enhancement
    and removed
    Issue-Question
    on Jan 16, 2021
  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-EnhancementResolution-No ActivityWG-Cmdlets

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions