Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mklement0 opened this issue May 2, 2018 · 3 comments
Closed
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@mklement0
Copy link
Contributor

mklement0 commented May 2, 2018

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
@BrucePay BrucePay added WG-Engine core PowerShell engine, interpreter, and runtime Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif labels May 2, 2018
@mklement0 mklement0 changed the title Member enumeration should not apply pipeline enumeration logic Member-access enumeration should not apply pipeline enumeration logic Oct 5, 2022
Copy link
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.

1 similar comment
Copy link
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.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
Copy link
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
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

2 participants