Repository navigation
Closed
Labels
Area-Maintainers-Documentationspecific to documentation in this repoIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.
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.CountActual 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:
, 1may be expected, but1is returned.- Note that it's fine not to wrap
, 1in an outer array if only one collection element's property value is found, because that is consistent with pipeline behavior.
- Note that it's fine not to wrap
- In the second case:
(1, 2), (3, 4)may be expected, but1, 2, 3, 4is 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)Metadata
Metadata
Assignees
Labels
Area-Maintainers-Documentationspecific to documentation in this repoIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.
Activity
BrucePay commentedon Mar 21, 2018
Per the linked document
$obj.prop.subPropis equivalent to$obj.prop | foreach subPropand so the individual member collections get flattened as expected. Here is a simplified example:Note that this behavior is currently documented in about_Properties. Do you think additional exposition is required?
mklement0 commentedon Mar 21, 2018
Bruce Payette (@BrucePay):
Thanks for pointing that out. The
$obj.prop | foreach-object subPropanalogy indeed is a good explanation of the behavior (( @{a=1, 2} , @{a=3, 4} ).a.Countyielding4rather than2).I also wasn't aware that
about_Propertiesalready talks about member enumeration, thanks.However, the linked page is a 6-year-old blog post, and
about_Propertiesdoes not discuss the flattening: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 subPropanalogy 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.