You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support a foreach statement with implicit iteration variable $_ for consistency with the ForEach-Object cmdlet #3830
The builtin foreachalias for the ForEach-Objectcmdlet keeps causing confusion with the foreachstatement.
Given that the cmdlet and the statement are conceptually analogous, perhaps one way to mitigate the confusion is to allow the foreachstatement to have an implied iteration variable, namely $_, just as when using ForEach-Object.
Desired behavior
# This works currently: an *explicit* iteration variable is needed.>foreach ($suffixin1,2,3) { "A"+$suffix }
A1
A2
A3
# Wishful thinking: allow *omitting* an iteration variable to use $_ by default,# as in a pipeline, which would be the equivalent of:# 1, 2, 3 | ForEach-Object { "A" + $_ }>foreach (1,2,3) { "A"+$_ }
A1
A2
A3
Currently, one is forced to choose between:
more concise, but decidedly slower pipeline syntax: 1, 2, 3 | ForEach-Object { "A" + $_ }
faster, but more verbose foreach syntax: foreach($suffix in 1, 2, 3) { "A" + $suffix }
The suggested foreach syntax evens the playing field and allows a solution that is both concise and performant.
As an aside - and arguably this an argument both for and against this enhancement - note that even the current documentation doesn't know the difference between the foreach statement and the ForEach-Object cmdlet - see MicrosoftDocs/PowerShell-Docs#1514
I still think the enhancement makes a lot of sense, but the documentation needs to be fixed too / either way.
The @PowerShell/powershell-committee has reviewed this and decided against this proposal.
We have two primary concerns:
There are important distinctions between ForEach-Object and the foreach statement and making them more similar makes it more difficult to teach these distinctions.
The bigger concern is that $_ will commonly refer to different objects in what looks like potentially the same scope:
foreach (Get-Process)
{
# The first $_ refers to a System.Diagnostics.Process# The second $_ refers to a System.Diagnostics.ProcessModule$_.Modules|% { $_.ModuleName }
}
A post-mortem, just to indulge reveries of a better world:
There are important distinctions between ForEach-Object and the foreach statement and making them more similar makes it more difficult to teach these distinctions.
Yes, there are, and the way to highlight these distinctions is to:
fix the documentation, as suggested.
(I get that it's too late for that) remove the unfortunate foreachalias
By contrast, implicit use of $_ is a commonality worth implemeting.
The bigger concern is that $_ will commonly refer to different objects in what looks like potentially the same scope:
The very same concern applies to ForEach-Object already:
Get-Process|ForEach-Object {
# The first $_ refers to a System.Diagnostics.Process# The second $_ refers to a System.Diagnostics.ProcessModule$_.Modules|% { $_.ModuleName }
}
Activity
iSazonov commentedon Aug 17, 2017
Steve Lee (@SteveL-MSFT) Is the language enhancement approved or we need PowerShell-Committee conclusion?
mklement0 commentedon Aug 18, 2017
As an aside - and arguably this an argument both for and against this enhancement - note that even the current documentation doesn't know the difference between the
foreachstatement and theForEach-Objectcmdlet - see MicrosoftDocs/PowerShell-Docs#1514I still think the enhancement makes a lot of sense, but the documentation needs to be fixed too / either way.
SteveL-MSFT commentedon Aug 28, 2017
Ilya (@iSazonov) my opinion is that this isn't big enough or controversial to warrant a RFC, but I'll have the committee review this
lzybkr commentedon Sep 6, 2017
The @PowerShell/powershell-committee has reviewed this and decided against this proposal.
We have two primary concerns:
ForEach-Objectand theforeachstatement and making them more similar makes it more difficult to teach these distinctions.$_will commonly refer to different objects in what looks like potentially the same scope:mklement0 commentedon Sep 7, 2017
A post-mortem, just to indulge reveries of a better world:
Yes, there are, and the way to highlight these distinctions is to:
fix the documentation, as suggested.
(I get that it's too late for that) remove the unfortunate
foreachaliasBy contrast, implicit use of
$_is a commonality worth implemeting.The very same concern applies to
ForEach-Objectalready:lzybkr commentedon Sep 7, 2017
Our hypothesis is that nested ForEach-Object is significantly less common than
ForEach-Objectinside a for statement.We have plenty of longstanding feedback, often conflicting, but the feedback on
$_is skewed negatively, so that plays into this decision as well.