Skip to content

Incorrect assertion that intrinsic Count/Length properties are available to all collection types #11613

@surfingoldelephant

Description

@surfingoldelephant

Prerequisites

  • Existing Issue: Search the existing issues for this repository. If there is an issue that fits your needs do not file a new one. Subscribe, react, or comment on that issue instead.
  • Descriptive Title: Write the title for this issue as a short synopsis. If possible, provide context. For example, "Typo in Get-Foo cmdlet" instead of "Typo."
  • Verify Version: If there is a mismatch between documentation and the behavior on your system, ensure that the version you are using is the same as the documentation. Check this box if they match or the issue you are reporting is not version specific.

Links

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_intrinsic_members#properties
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_properties#examples

Summary

The documentation makes the following incorrect assertions:

The Count and Length properties are available to all PowerShell objects, not just collections.

All collections have a Count property that returns the number of objects in the collection.

PowerShell makes the intrinsic Count/Length properties available to scalar objects only. The properties are not made available intrinsicly to objects whose type is considered a collection by PS.

As not all collections have their own type-native Count or Length (or either), these properties are not available to all PowerShell objects/all collections.

Details

PS's rules/special-casing for what it considers a collection/scalar can be found here and here.

This comment contains code to check for the absence of Count and Length properties with commonly used collection types.

Suggested Fix

Make the following adjustments to about_Intrinsic_Members and about_Properties:

  • Remove the assertions that Count/Length properties are available to all objects and to all collections.
  • State that Count/Length properties are intrinsicly made available by PS if:
    • PS considers the object scalar.
    • The scalar object does not already have a Count/Length property.
  • Note that $null is scalar and has an intrinsic Count/Length of 0. Likewise, an uninitialized variable is implicitly $null, so it too has an intrinsic Count/Length of 0.
  • State that aside from arrays, a PS user will typically encounter collections with only a Count property (no Length).

In addition:

Metadata

Metadata

Assignees

Labels

area-aboutArea - About_ topicsissue-doc-bugIssue - error in documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions