-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Compare-Object
doesn't compare custom value types (structs
) (that don't implement IComparable
) correctly
#23805
Comments
With objects a good practice is using parameter |
How about the following, then? Compare-Object @{ Instance = $instance1 } @{ Instance = $instance2 } -Property Instance Still broken. The point is that it doesn't matter whether the problem arises in the context of whole-object or property-value comparisons. Sure, if you know of a distinguishing property - assuming there even is one - you can use it as a workaround |
Broken because "$instance1"
Foo
"$instance2"
Foo So But Compare-Object ([pscustomobject]@{ Instance = $instance1 }) ([pscustomobject]@{ Instance = 24545 }) -Property Instance
Instance SideIndicator
-------- -------------
24545 =>
Foo <= works
|
No, it's broken for the reasons stated in the initial post.
The other examples are irrelevant to this discussion, and as for the "If it can't find a suitable method" quote from the docs: For value types (types derived from |
Compare-Object
doesn't compare custom value types (structs
) that don't implement IComparable
correctlyCompare-Object
doesn't compare custom value types (structs
) (that don't implement IComparable
) correctly
Prerequisites
Steps to reproduce
Because
Compare-Object
doesn't respect afalse
return value when the.Equals()
method is called, it falls back to string comparison, which, with the default.ToString()
implementation, results in false positives.However, for instances of value types, a
false
return value should be trusted, given that a value equality test is performed.(This only applies if they don't implement
IComparable
; if they do, the latter is used.)Related:
Expected behavior
The two instances should be considered unequal, and produce something like:
Note: With the default
.ToString()
implementation, both instances stringify to their (full) type name, so the reason for their inequality doesn't show here (and this default.ToString()
implementation is also the cause of the false positives).Actual behavior
No output. That is, due to the inappropriate string-comparison fallback (which results in comparing two
'Foo'
strings),Compare-Object
unexpectedly considered these value-type instances equal, even though they're clearly not (as$instance1 -eq $instance2
shows).Error details
No response
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: