Skip to content

Member enumeration unwraps and flattens array-valued properties #6454

Closed

Description

Note:

  • The behavior described below has been in place since member enumeration was introduced in v3 and only affects array-valued properties.
  • The behavior may well be by design, but given that it can be surprising, it may need to be documented.

Steps to reproduce

# Create an object whose .prop value is an array of hashtables, only one of 
# which as a .subProp entry whose value is a single-element array, then
# access .subProp via member enumeration.
$obj=[pscustomobject] @{ prop = @{foo='foo'}, @{subProp=,1}}; ($obj.prop.subProp).GetType().Name

# Create two 2-element arrays and let member enumeration collect them.
$obj=[pscustomobject] @{prop = @{subProp=1,2}, @{subProp=3,4}}; $obj.prop.subProp.Count

Actual behavior

Int32
4

The single-element array was unwrapped.
The two 2-element arrays were concatenated (rather than collected as-is in a 2-element array).

Alternative behavior

One could expect the following behavior, and contrasting it with the actual behavior is worth documenting:

Object[]
2

The value of the properties could have been passed through, just as with direct property access (e.g., $obj.prop[-1].subProp):

  • In the first case: , 1 may be expected, but 1 is returned.
    • Note that it's fine not to wrap , 1 in an outer array if only one collection element's property value is found, because that is consistent with pipeline behavior.
  • In the second case: (1, 2), (3, 4) may be expected, but 1, 2, 3, 4 is returned (the equivalent of (1, 2) + (3, 4))

Environment data

PowerShell Core v6.0.2 on macOS 10.13.3
PowerShell Core v6.0.2 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. added
    Area-Maintainers-Documentation
    Issue-Discussion
    on Mar 21, 2018
  2. BrucePay commented on Mar 21, 2018

    @BrucePay
    Collaborator

    Per the linked document $obj.prop.subProp is equivalent to $obj.prop | foreach subProp and so the individual member collections get flattened as expected. Here is a simplified example:

    PS[1] (67) > ( @{a=1, 2} , @{a=3, 4} ).a
    1
    2
    3
    4
    

    Note that this behavior is currently documented in about_Properties. Do you think additional exposition is required?

  3. mklement0 commented on Mar 21, 2018

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay):

    Thanks for pointing that out. The $obj.prop | foreach-object subProp analogy indeed is a good explanation of the behavior (( @{a=1, 2} , @{a=3, 4} ).a.Count yielding 4 rather than 2).

    I also wasn't aware that about_Properties already talks about member enumeration, thanks.

    However, the linked page is a 6-year-old blog post, and about_Properties does not discuss the flattening:

    PowerShell returns the value of that property for every object in the collection.

    That phrasing could easily lead one to expect the alternative behavior.

    Therefore, I suggest amending about_Properties [update: created this doc issue]:

    • by adding the $obj.prop | foreach-object subProp analogy to it.

    • mentioning the feature's name: "member enumeration", assuming that should be the official term; it always helps to have a name for a feature.

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

    Area-Maintainers-DocumentationIssue-DiscussionResolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions