Title: Support a foreach statement with implicit iteration variable $_ for consistency with the ForEach-Object cmdlet · Issue #3830 · PowerShell/PowerShell · GitHub
Open Graph Title: Support a foreach statement with implicit iteration variable $_ for consistency with the ForEach-Object cmdlet · Issue #3830 · PowerShell/PowerShell
X Title: Support a foreach statement with implicit iteration variable $_ for consistency with the ForEach-Object cmdlet · Issue #3830 · PowerShell/PowerShell
Description: Inspired by #3695. The builtin foreach alias for the ForEach-Object cmdlet keeps causing confusion with the foreach statement. Given that the cmdlet and the statement are conceptually analogous, perhaps one way to mitigate the confusion ...
Open Graph Description: Inspired by #3695. The builtin foreach alias for the ForEach-Object cmdlet keeps causing confusion with the foreach statement. Given that the cmdlet and the statement are conceptually analogous, pe...
X Description: Inspired by #3695. The builtin foreach alias for the ForEach-Object cmdlet keeps causing confusion with the foreach statement. Given that the cmdlet and the statement are conceptually analogous, pe...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3830
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support a foreach statement with implicit iteration variable $_ for consistency with the ForEach-Object cmdlet","articleBody":"\u003csup>Inspired by #3695.\u003c/sup>\r\n\r\nThe builtin `foreach` _alias_ for the `ForEach-Object` _cmdlet_ keeps causing confusion with the `foreach` _statement_.\r\n\r\nGiven that the cmdlet and the statement are conceptually analogous, perhaps one way to mitigate the confusion is to allow the `foreach` _statement_ to have an _implied_ iteration variable, namely `$_`, just as when using `ForEach-Object`.\r\n\r\nDesired behavior\r\n-----------------\r\n\r\n```powershell\r\n# This works currently: an *explicit* iteration variable is needed.\r\n> foreach ($suffix in 1, 2, 3) { \"A\" + $suffix }\r\nA1\r\nA2\r\nA3\r\n\r\n# Wishful thinking: allow *omitting* an iteration variable to use $_ by default,\r\n# as in a pipeline, which would be the equivalent of:\r\n# 1, 2, 3 | ForEach-Object { \"A\" + $_ }\r\n> foreach (1, 2, 3) { \"A\" + $_ }\r\nA1\r\nA2\r\nA3\r\n```\r\n\r\nCurrently, one is forced to choose between:\r\n\r\n* more concise, but decidedly _slower_ pipeline syntax: `1, 2, 3 | ForEach-Object { \"A\" + $_ }`\r\n\r\n* faster, but more verbose `foreach` syntax: `foreach($suffix in 1, 2, 3) { \"A\" + $suffix }`\r\n\r\nThe suggested `foreach` syntax evens the playing field and allows a solution that is both concise _and_ performant.\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-beta (v6.0.0-beta.1) \r\n```\r\n\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-05-19T23:07:39Z","url":"https://github.com/PowerShell/PowerShell/issues/3830"}
| 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:4dbd2c61-6b3b-7f66-b212-b296c831e4f2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8AFE:D2092:6F8EF1:9FE0B5:6AFD91EA |
| html-safe-nonce | 92e9de9e97799901cf332d8d9bda1240bc1acc52971334adf96be912c73a099d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUZFOkQyMDkyOjZGOEVGMTo5RkUwQjU6NkFGRDkxRUEiLCJ2aXNpdG9yX2lkIjoiMTAzNDQ1NTE1ODY0MzA2OTQxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6df28745b6d470809cc7a22d56a73ce369d10233c86db189429b2e3ce1f1392a |
| hovercard-subject-tag | issue:230109702 |
| 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/3830/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1a6c075bf6ab137ab74f9214ede6375ae771053b0f0bf27d66f765db5c30df26/PowerShell/PowerShell/issues/3830 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1a6c075bf6ab137ab74f9214ede6375ae771053b0f0bf27d66f765db5c30df26/PowerShell/PowerShell/issues/3830 |
| og:image:alt | Inspired by #3695. The builtin foreach alias for the ForEach-Object cmdlet keeps causing confusion with the foreach statement. Given that the cmdlet and the statement are conceptually analogous, pe... |
| 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