Title: Compare-Object doesn't respect IEquatable
Open Graph Title: Compare-Object doesn't respect IEquatable
X Title: Compare-Object doesn't respect IEquatable
Description: Input objects that implement IEquatable
Open Graph Description: Input objects that implement IEquatable
X 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 require...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/12498
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Compare-Object doesn't respect IEquatable\u003cT>","articleBody":"Input objects that implement `IEquatable\u003cT>` but not also `IComparable\u003cT>` aren't properly compared in terms of equality.\r\n\r\nThe 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\u003cT>.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.\r\n\r\nSee https://github.com/PowerShell/PowerShell/blob/16ec1cc32f9e60c9d76b074c865c32a5d2acf5a2/src/System.Management.Automation/engine/LanguagePrimitives.cs#L883 and https://github.com/PowerShell/PowerShell/blob/16ec1cc32f9e60c9d76b074c865c32a5d2acf5a2/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ObjectCommandComparer.cs#L222\r\n\r\nRelated:\r\n\r\n* #23805\r\n\r\n# Steps to reproduce\r\n\r\n```powershell\r\n# Define class Foo that wraps an integer and implements IEquatable\u003cT>\r\n# to return true if two instances wrap the same integer value.\r\nAdd-Type 'public class Foo : System.IEquatable\u003cFoo> { 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; } }'\r\n\r\n$o1 = [Foo]::new(); $o1.Bar = 1\r\n$o2 = [Foo]::new(); $o2.Bar = 2\r\n\r\n# OK\r\n$o1.Equals($o2) | Should -BeFalse\r\n\r\n# FAILS, because $o1 and $o2 are unexpectedly considered equal.\r\nCompare-Object $o1 $o2 | Should -Not -BeNullOrEmpty\r\n```\r\n\r\n# Expected behavior\r\n\r\nBoth tests should succeed.\r\n\r\n# Actual behavior\r\n\r\nThe 2nd test fails, because `$o1` and `$o2` are unexpectedly considered equal, so `Compare-Object` produces no output. \r\n\r\n```none\r\nExpected a value, but got $null or empty.\r\n```\r\n\r\n# Environment data\r\n\r\n```none\r\nPowerShell Core 7.1.0-preview.2\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2020-04-26T19:49:07Z","url":"https://github.com/PowerShell/PowerShell/issues/12498"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8ab3618c-eecc-b9cd-d041-54c342c9f4d8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC76:3042FD:3E241C:595DF9:6AFD6DCE |
| html-safe-nonce | 9f5e7adbf24b1848c4e56a2d08a279a791a8b8a8dfcfde43932f93a07cb3eebe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzc2OjMwNDJGRDozRTI0MUM6NTk1REY5OjZBRkQ2RENFIiwidmlzaXRvcl9pZCI6IjU0MDAzNjQ0MDA4NjEwODEwMzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ccf20fd5bc73334b2b2ce1fdf35496e3df7954fb759890cc7b5501b19681ad47 |
| hovercard-subject-tag | issue:607097903 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/12498/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fdb3f2b83e1fad895021e414a655dfe260d0acbeb67ce33937f6745e47824b51/PowerShell/PowerShell/issues/12498 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fdb3f2b83e1fad895021e414a655dfe260d0acbeb67ce33937f6745e47824b51/PowerShell/PowerShell/issues/12498 |
| og:image:alt | Input objects that implement IEquatable |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9985ca2e21b59673104d4d830d4afeb946c67f248f273ab2e824c31c8db46e4a |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0db9e8d463c3f4540570e6ad3c501a06db9dc02a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width