Skip to content

Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property, as do some objects that are implicitly treated as collections #6456

Closed

Description

[Superseded by #6466]

#3671 discussed the issue with respect to [pscustomobject] instances, for which a fix was attempted in #5756 (but the fix is not in v6.0.2 - Ilya (@iSazonov), should it be?).

However, there are instances of other types where the problem persists.

It would be good to clarity on the generic issue, so that it can be fixed for any (current and future) type.

Steps to reproduce

([System.Data.DataTable]::new()).Count
([pscustomobject] @{}).Count

Expected behavior

0   # because [System.Data.DataTable] is treated as an *enumerable* - see @stknohg's comment below.
1

Actual behavior

The output is $null, because there is no .Count property.

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. changed the title [-]Treating scalars implicitly as collections doesn't with all objects - some lack a .Count property[/-] [+]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property[/+] on Mar 22, 2018
  2. stknohg commented on Mar 22, 2018

    @stknohg
    Contributor

    DataTable is defined as enumerable in PowerShell.

    I think

    ([System.Data.DataTable]::new()).Count

    should retruns 0.

    but this expression returns nothing currently...
    I think it is a bug.


    workaround?

    ([System.Data.DataTable]::new().Rows).Count

    returns 0 as expected.

  3. mklement0 commented on Mar 22, 2018

    @mklement0
    ContributorAuthor

    Takuya Shibata (@stknohg): Thanks, good point about [System.Data.DataTable] being enumerable (though seemingly not in all contexts (anymore) - I've updated the original post.

    Yes, it should return 0 in this case (or, generally, however many rows the .Rows property contains) which, as you state, doesn't work.

  4. changed the title [-]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property[/-] [+]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property, as do some objects treated implicitly as collections[/+] on Mar 22, 2018
  5. changed the title [-]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property, as do some objects treated implicitly as collections[/-] [+]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property, as do some objects that are treated implicitly as collections[/+] on Mar 22, 2018
  6. changed the title [-]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property, as do some objects that are treated implicitly as collections[/-] [+]Treating scalars implicitly as collections doesn't work with all objects - some lack a .Count property, as do some objects that are implicitly treated as collections[/+] on Mar 22, 2018
  7. iSazonov commented on Mar 22, 2018

    @iSazonov
    Collaborator

    v6.0.2 is only security update.

  8. added
    WG-Language
    on Mar 22, 2018
  9. iSazonov commented on Mar 22, 2018

    @iSazonov
    Collaborator

    Is the Issue for ForEach and Where too?

  10. mklement0 commented on Mar 22, 2018

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov): Thanks; your [pscustomobject] fixes were indeed effective in all respects (.Count, [0], .ForEach(), .Where()), I checked with the latest code. The issue with [DataTable] is a different one that I'll address in a new issue.

  11. added
    Issue-Discussion
    Resolution-Answered
    on Mar 24, 2018
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

    Issue-DiscussionResolution-AnsweredWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions