Skip to content

Treating scalars implicitly as collections doesn't fully work with custom objects ([pscustomobject]) - lacks a .Count property #3671

@mklement0

Description

@mklement0

Treating scalars as if they were collections is a powerful feature that eliminated many bugs when it was introduced in PSv3: it means that even a single object returns meaningful values for .Count and .Length and allows indexing with [0].

Unfortunately, not all scalar objects behave this way, a notable exception being custom objects ([pscstomobject]), such as the instances created by Select-Object.

The problem is the absence of the .Count / .Length properties.
Similarly, using collection operators .Where() and .ForEach() also does not work.

By contrast, indexing custom objects with [0] does work.

I wonder if there are other cases where cmdlets return objects (other than [pscustomobject] instances) that behave the same way.

Steps to reproduce

$null.Count
(Get-Item /).Count
(Get-Item / | Select-Object Name).Count  # !! produces no output
([pscustomobject] @{ foo = 'bar' }).Count # !! produces no output

Expected behavior

0
1
1
1

Actual behavior

0
1

^ 3rd and 4th command didn't produce output, because the implicitly and explicitly created [pscustomobject] instances have no .Count and .Length properties.

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Languageparser, language semantics

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions