Skip to content

Member-access enumeration should not apply pipeline enumeration logic #6802

Closed

Description

Note:


Currently, <objOrCollection>.<member> is equivalent to
<objOrCollection> | ForEachObject { $_.<member> }.
That is, pipeline logic is applied when collecting the member values from a collection's elements:

# A member collected from a single-element collection is unwrapped.
# The following returns *scalar* 1:
PS> (, @{ a = 1 }).a.GetType().Name
Int32    # suggested alternative: (, 1) (single-element array)

# Collecting array-valued members *concatenates* the arrays.
# The following returns *flat* array 1, 2, 3, 4:
PS> (@{ a = 1, 2 }, @{ a = 3, 4 }).a.Count
4 # suggested alternative: (1, 2), (3, 4); a 2-element array containing the member arrays as-is

The current behavior is problematic for the following reasons:

  • It is generally surprising that pipeline logic is invisibly applied, given that member enumeration applies to expression mode only.

  • Intuitively, one would expect member values to be collected as-is, not to be concatenated if they happen to be array-valued.

  • The unwrapping of a single member value is especially counterintuitive in combination with @(...):

$objOrColl =  @{ Name = 'foo' }

# Ensure that $objOrColl is *array*-valued with @(...) so that -like acts as an array *filter*.
# Currently the use of .Name negates the effect of @(...):
@($objOrColl).Name -like 'f*'
True # !! LHS evaluated to a *scalar* and returns a Boolean rather than the intended 'foo'

Enclosing the entire expression in @(...) solves the problem, but that's not obvious and shouldn't be necessary: @($objOrColl.Name) -like 'f*'


Additionally, in the no-input-object case, member enumeration returns $null rather than [System.Management.Automation.Internal.AutomationNull]::Value, which can also lead to surprising behavior:

$pids = @((Get-Process *NoSuchProcess*).Id)

The intuitive expectation is for $pids to contain an empty array, but, because $null was output by the member enumeration, the actual result is @($null), i.e. a single-element array containing $null.

Environment data

Written as of:

PowerShell Core v6.0.2

Activity

  1. added
    WG-Enginecore PowerShell engine, interpreter, and runtime
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on May 2, 2018
  2. changed the title [-]Member enumeration should not apply pipeline enumeration logic[/-] [+]Member-access enumeration should not apply pipeline enumeration logic[/+] on Oct 5, 2022
  3. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions