Skip to content

Support a foreach statement with implicit iteration variable $_ for consistency with the ForEach-Object cmdlet #3830

Closed

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 is to allow the foreach statement 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 ($suffix in 1, 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.

PowerShell Core v6.0.0-beta (v6.0.0-beta.1) 

Activity

  1. added
    WG-Language
    Issue-Enhancement
    on May 20, 2017
  2. iSazonov commented on Aug 17, 2017

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) Is the language enhancement approved or we need PowerShell-Committee conclusion?

  3. mklement0 commented on Aug 18, 2017

    @mklement0
    ContributorAuthor

    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.

  4. SteveL-MSFT commented on Aug 28, 2017

    @SteveL-MSFT
    Member

    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

  5. added
    Review - Committee
    on Aug 28, 2017
  6. lzybkr commented on Sep 6, 2017

    @lzybkr
    Contributor

    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 }
    }
  7. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Sep 6, 2017
  8. mklement0 commented on Sep 7, 2017

    @mklement0
    ContributorAuthor

    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 foreach alias

    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 }
    }
  9. lzybkr commented on Sep 7, 2017

    @lzybkr
    Contributor

    Our hypothesis is that nested ForEach-Object is significantly less common than ForEach-Object inside 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.

  10. added
    Resolution-Won't Fix
    on Sep 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedIssue-EnhancementResolution-Won't FixWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions