Title: Expose the .ForEach() and .Where() methods as regular PowerShell operators · Issue #6576 · PowerShell/PowerShell · GitHub
Open Graph Title: Expose the .ForEach() and .Where() methods as regular PowerShell operators · Issue #6576 · PowerShell/PowerShell
X Title: Expose the .ForEach() and .Where() methods as regular PowerShell operators · Issue #6576 · PowerShell/PowerShell
Description: Note: This proposal arose out of the discussion in #6512 An RFC would have to be written, but I first want to see if there's a fundamental willingness to implement this. The hidden and effectively undocumented .Where() and .ForEach metho...
Open Graph Description: Note: This proposal arose out of the discussion in #6512 An RFC would have to be written, but I first want to see if there's a fundamental willingness to implement this. The hidden and effectively ...
X Description: Note: This proposal arose out of the discussion in #6512 An RFC would have to be written, but I first want to see if there's a fundamental willingness to implement this. The hidden and effectiv...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/6576
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Expose the .ForEach() and .Where() methods as regular PowerShell operators","articleBody":"Note: \r\n* This proposal arose out of the discussion in #6512 \r\n* An RFC would have to be written, but I first want to see if there's a fundamental willingness to implement this.\r\n\r\n---\r\n\r\nThe ~~hidden~~ and effectively undocumented `.Where()` and `.ForEach` _methods_ available on every object exposed by PowerShell were introduced to support DSC in v4 and provide functionality similar to the `Where-Object` and `ForEach-Object` cmdlets, but (a) in a more performant way (albeit in an all-in-memory way) and (b) with more features. The most comprehensive documentation that I'm aware of is [this blog post by @KirkMunro](http://www.powershellmagazine.com/2014/10/22/foreach-and-where-magic-methods/).\r\n\r\nWhile they could simple be documented (and made more discoverable), **I suggest surfacing them as bona fide PowerShell _operators_ `-foreach` and `-where`**:\r\n\r\n* No _native_ PowerShell features that I am aware of are exposed as _methods_: instead, functionality is surfaced as _commands_ (cmdlets/functions) and _operators_.\r\n\r\n * Methods belong to a _different realm_, to which [`Get-Help about_Methods`](https://github.com/PowerShell/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Methods.md) is the portal (albeit one that, unfortunately, currently doesn't discuss syntax pitfalls and doesn't even reference [`Get-Help about_Parsing`](https://github.com/PowerShell/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Parsing.md)); it's a powerful realm and definitely worth knowing about, but it is a different realm, distinct from how PowerShell itself surfaces its functionality.\r\n\r\n* The `.Where()` and `.ForEach()` methods were introduced as such for the benefit of _DSC_ - ~~of necessity, I presume, given that the DSC DSL doesn't allow use of _pipelines_~~ (see comments below).\r\n\r\n * Despite their general availability inside PowerShell itself, they're effectively undocumented, and many people may not even be aware of their existence (since ~~tab completion doesn't help~~ (see comments below), and neither `Get-Member` nor `.psobject.Methods` find them, you simply have to _know_ of their existence).\r\n\r\n * Even if fully documented (and, as discussed, this is currently even lacking in the _DSC_ context, and overall Kirk's blog post is still the most complete documentation), these methods make awkward tools in general PowerShell use, due to their method syntax and output data types.\r\n\r\nHence my suggestion to **surface them as _operators_ with `[object[]]` output**, which would make them discoverable interactively (`-\u003ctab>`) , and they would be documented alongside the existing operators.\r\n\r\nThey'd make **concise complements to their cmdlet counterparts**; `-foreach` would make the `foreach` _loop_ unnecessary in many situations and with its use of `$_` reduce the existing confusion between the loop and the cmdlet (see https://github.com/PowerShell/PowerShell-Docs/issues/1514 and a rejected attempt to harmonize the two, #3830):\r\n\r\n```powershell\r\n$var = 1, 2, 3 -foreach { $_ + 1 } # wishful thinking\r\n\r\n# vs.\r\n\r\n$var = foreach ($i in 1, 2, 3) { $i + 1 }\r\n# or (slower)\r\n$var = 1, 2, 3 | ForEach-Object { $_ + 1 }\r\n\r\n# ---\r\n\r\n$var = 1, 2, 3 -where { $_ -gt 1 } # wishful thinking\r\n\r\n# vs.\r\n# (slower)\r\n$var = 1, 2, 3 | Where-Object { $_ -gt 1 }\r\n# or\r\n$var = foreach ($i in 1, 2, 3) { if ($i -gt 1) { $i } }\r\n\r\n```\r\n\r\nArguably, even DSC could benefit from the cleaner operator syntax; contrast the following:\r\n\r\n```powershell\r\n# Note that this example, taken from https://docs.microsoft.com/en-us/powershell/dsc/separatingenvdata\r\n# doesn't even use `()` around the script-block argument, so the use of Where() \r\n# isn't even readily recognizable as a *method* call.\r\nNode ($AllNodes.Where{$_.Role -eq \"WebServer\"}).NodeName { ...\r\n\r\n# vs.\r\n\r\nNode ($AllNodes -where {$_.Role -eq \"WebServer\"}).NodeName { ... # wishful thinking\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\nWritten as of:\r\n\r\n```powershell\r\nPowerShell Core v6.0.2\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2018-04-05T20:08:01Z","url":"https://github.com/PowerShell/PowerShell/issues/6576"}
| 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:e290b143-b52b-d955-626b-5b0a2c5b4eec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8574:338C0B:6407EC:9143A3:6AFD8D60 |
| html-safe-nonce | e5e1235c8f74c125ab9a5a6d5cc163ef8688212e5e38734fa5f616d16939ef33 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTc0OjMzOEMwQjo2NDA3RUM6OTE0M0EzOjZBRkQ4RDYwIiwidmlzaXRvcl9pZCI6IjMyMjA1MTMyODY2MTI5NDYyNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e62988b49666e33a10bf93bcd405b3b043b982e3466aaf856b2a58e52df0538c |
| hovercard-subject-tag | issue:311751430 |
| 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/6576/issue_layout |
| twitter:image | https://opengraph.githubassets.com/df44302c886565c01930a3c0d6da1018162d19eeed98a3706228f2ec99d9acba/PowerShell/PowerShell/issues/6576 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/df44302c886565c01930a3c0d6da1018162d19eeed98a3706228f2ec99d9acba/PowerShell/PowerShell/issues/6576 |
| og:image:alt | Note: This proposal arose out of the discussion in #6512 An RFC would have to be written, but I first want to see if there's a fundamental willingness to implement this. The hidden and effectively ... |
| 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