Title: `Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly · Issue #23805 · PowerShell/PowerShell · GitHub
Open Graph Title: `Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly · Issue #23805 · PowerShell/PowerShell
X Title: `Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly · Issue #23805 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/23805
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nBecause `Compare-Object` doesn't respect a `false` return value when the `.Equals()` method is called, it falls back to _string_ comparison, which, with the default `.ToString()` implementation, results in false positives.\r\n\r\nHowever, for instances of _value types_, a `false` return value _should_ be trusted, given that a _value_ equality test is performed.\r\n(This only applies if they don't implement `IComparable`; if they do, the latter is used.)\r\n\r\n```powershell\r\nAdd-Type @'\r\npublic struct Foo {\r\n public int Num;\r\n}\r\n'@\r\n\r\n$instance1, $instance2 = [Foo]::new(), [Foo]::new()\r\n\r\n# Make one instance different\r\n$instance1.Num = 1\r\n\r\n# Compare them.\r\n# !! This currently produces NO output, even though the two instances\r\n# !! are clearly different ($instance1 -eq $instance2 yields $false)\r\nCompare-Object $instance1 $instance2\r\n```\r\n\r\n---\r\n\r\nRelated:\r\n\r\n * #12498\r\n\r\n### Expected behavior\r\n\r\nThe two instances should be considered unequal, and produce something like:\r\n\r\n\r\n```console\r\nInputObject SideIndicator\r\n----------- -------------\r\nFoo =>\r\nFoo \u003c=\r\n```\r\n\r\nNote: 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).\r\n\r\n\r\n### Actual behavior\r\n\r\nNo 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).\r\n\r\n\r\n### Error details\r\n\r\n_No response_\r\n\r\n### Environment data\r\n\r\n```powershell\r\nPowerShell 7.5.0-preview.2\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2024-05-15T14:22:42Z","url":"https://github.com/PowerShell/PowerShell/issues/23805"}
| 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:466d0c72-66fd-554d-7588-c028decd8649 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC78:21306C:22B5E5:306730:6AFD8264 |
| html-safe-nonce | 491640083bb7372017b182dfa2786541a366109705faa0f440592a44a9dfca3c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzc4OjIxMzA2QzoyMkI1RTU6MzA2NzMwOjZBRkQ4MjY0IiwidmlzaXRvcl9pZCI6IjUzNzExMzgwODA0MTk3MDk1NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a4a674a57fb0610286f80322b2da0368d5b66fea98275f7adc191a9149ffaaf0 |
| hovercard-subject-tag | issue:2298085382 |
| 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/23805/issue_layout |
| twitter:image | https://opengraph.githubassets.com/276d4b11253cf617bec23c84710164f289c62f4c79cc46ec4d2d006de0273a49/PowerShell/PowerShell/issues/23805 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/276d4b11253cf617bec23c84710164f289c62f4c79cc46ec4d2d006de0273a49/PowerShell/PowerShell/issues/23805 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| 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 | 42d4967e3c8afca51ccac31f419a705af777226a95f025ea73387d87a306b326 |
| 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 | 2329ceed0bc2c7b7c3b9f5f2db6abfc7b6fc3a50 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width