-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
Treating scalars implicitly as collections doesn't fully work with custom objects ([pscustomobject]) - lacks a .Count property #3671
Comments
It is behavior of ([PSCustomObject]@{q=1}).Count
<empty>
([PSObject]@{q=1}).Count
1
|
Thanks for the clarification - that simplifies the issue. |
Where can I start? |
This is also an issue for
Other single objects and null have support for this method.
I suspect they are the same underlying issue is why I didn't create a new issue for this. |
@lzybkr Could you please help - where is the magic code? |
I think the magic code is here but I'd need to debug to see what's going wrong because I don't see the bug with code inspection. |
The code isn't called at all 😕 |
@iSazonov - I don't normally write code that isn't called :) It is possible that the code I pointed to isn't reachable for I will also point out - most of the code in binders.cs is generating code for an operation which is cached, so if some binding happened before you set a breakpoint, you'll never hit it again. For example, if you attach after startup, it's possible loading a module or even evaluating your prompt hit the code and you won't hit it again. Starting the process under the debugger helps, or you can resort to adding some trace output or a message box so you can attach and debug. |
😄 Yes, I meant I discover a difference in
If I change canOptimize to true in debuger I get right output - Count = 1. I don't know that is right fix. |
That is not the right fix. I think the right fix is to change |
Related #3671 •Add Count and Length properties to [PSCustomobject]. Now following returns 1: ([pscustomobject] @{ foo = 'bar' }).Count ([pscustomobject] @{ foo = 'bar' }).Length •Add tests
First part was fixed - ([pscustomobject] @{ foo = 'bar' }).Count returns 1. |
@iSazonov - the change will be similar to what you did for |
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 bySelect-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
Expected behavior
Actual behavior
^ 3rd and 4th command didn't produce output, because the implicitly and explicitly created
[pscustomobject]
instances have no.Count
and.Length
properties.Environment data
The text was updated successfully, but these errors were encountered: