Title: Collection operators .Where() and .ForEach() do not unwrap single-item results · Issue #6512 · PowerShell/PowerShell · GitHub
Open Graph Title: Collection operators .Where() and .ForEach() do not unwrap single-item results · Issue #6512 · PowerShell/PowerShell
X Title: Collection operators .Where() and .ForEach() do not unwrap single-item results · Issue #6512 · PowerShell/PowerShell
Description: The PSv4+ collection operators .Where() and .ForEach() differ from normal pipeline-output collecting behavior in two respects: If the result is a single item, it isn't unwrapped; that is, a collection is always returned, and that collect...
Open Graph Description: The PSv4+ collection operators .Where() and .ForEach() differ from normal pipeline-output collecting behavior in two respects: If the result is a single item, it isn't unwrapped; that is, a collect...
X Description: The PSv4+ collection operators .Where() and .ForEach() differ from normal pipeline-output collecting behavior in two respects: If the result is a single item, it isn't unwrapped; that is, a col...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/6512
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Collection operators .Where() and .ForEach() do not unwrap single-item results","articleBody":"The PSv4+ collection operators `.Where()` and `.ForEach()` differ from normal pipeline-output collecting behavior in two respects:\r\n\r\n* If the result is a _single_ item, it _isn't unwrapped_; that is, a _collection_ is _always_ returned, and\r\n* that collection is always of type `[System.Collections.ObjectModel.Collection[psobject]]` rather than a regular PS array (`[object[]]`)\r\n\r\nIn the spirit of https://github.com/PowerShell/PowerShell-RFC/pull/120, the **behavior should be harmonized with how pipeline output is already collected and how the `-*Variable` common parameters (except `-PipelineVariable`) _should_ collect output, according to the RFC**.\r\n\r\nGiven how contentious the linked `-OutVariable`-related RFC is turning out to be, this proposal may need to become an RFC as well, though perhaps the decision on said RFC will imply how to proceed here.\r\n\r\nTo reiterate what, to me, motivates this desire for harmonization / resolving inconsistencies:\r\n\r\n**The unifying vision is to provide a consistent experience whenever PowerShell collects output for you**, so you never need to worry about different unwrapping behavior and/or differing collection types.\r\n\r\nNote:\r\n\r\n**This harmonization would currently incur a performance cost**, because the internally used, resizable collection data types must on output be converted to `object[]]`.\r\n\r\nHowever, **the proper way to resolve this is to switch PowerShell's fundamental collection data type from `[object[]]` to an _efficiently resizable_ one that can be used internally _and_ directly output.**\r\n[Discussion in that direction has started](https://github.com/PowerShell/PowerShell/issues/5643#issuecomment-349811857), but it's obviously a massive _internal_ change; _externally_, assuming no bugs have crept in, however, much of existing code may not only continue to work as-is, but may exhibit automatic performance gains.\r\n\r\nTo quote @lzybkr from the linked issue:\r\n\r\n> I've often wondered if the comma operator could create a list instead of an array. I have a feeling most scripts would never notice a difference because of how freely things are converted to an object array.\r\n\r\nCurrent Behavior\r\n-----------------\r\n\r\n```powershell\r\nPS> (1).ForEach({$_}).GetType().Name; (1, 2).ForEach({$_}).GetType().Name\r\nCollection`1 # single-item result wasn't unwrapped\r\nCollection`1 # output collection type is not [object[]]\r\n```\r\n\r\nDesired Behavior\r\n-----------------\r\n\r\n```powershell\r\nPS> (1).ForEach({$_}).GetType().Name; (1, 2).ForEach({$_}).GetType().Name\r\nInt32 # single-item result was unwrapped\r\nObject[] # regular PS array\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2018-03-27T18:03:17Z","url":"https://github.com/PowerShell/PowerShell/issues/6512"}
| 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:f76a86d6-241d-cbb2-ef7a-1b36469931d3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA8E:24FA16:77BF7:A32AF:6AFD8AB3 |
| html-safe-nonce | 8a408911ed56ad569a7134a896c3f0d1699c4041e1671160c2e163efe7843f98 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQThFOjI0RkExNjo3N0JGNzpBMzJBRjo2QUZEOEFCMyIsInZpc2l0b3JfaWQiOiI2Njc1Njk5NTExODMwNDczNDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 332481ef529391c6270abeba8450797a8935caa80a3bd90ab922a73ae0d9b59b |
| hovercard-subject-tag | issue:309074479 |
| 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/6512/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eb13830c7f5a3b9816422e413af86d7c69cbf0f7d9ab9986b68a2b169aa7f85a/PowerShell/PowerShell/issues/6512 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eb13830c7f5a3b9816422e413af86d7c69cbf0f7d9ab9986b68a2b169aa7f85a/PowerShell/PowerShell/issues/6512 |
| og:image:alt | The PSv4+ collection operators .Where() and .ForEach() differ from normal pipeline-output collecting behavior in two respects: If the result is a single item, it isn't unwrapped; that is, a collect... |
| 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 | bc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea |
| 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 | 927a6c5ab1c32b38830f1b9d3050c0b9e9aa92e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width