Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object #4257

Copy link
Copy link
@mklement0

Description

@mklement0
Issue body actions

tl; dr:

This issue is written based on the following, currently unfulfilled expectation:

  • When you wrap a New-Object call that outputs an array / collection in @(), it should not create an array wrapper around it.

@PetSerAl disagrees with my expectation (quotes from the comments on this SO post that inspired this issue, part of which is reprinted below):

What is unexpected about this behavior? New-Object writes a single element to the pipeline and @() wraps it in an array.

On the tangentially related issue that @() preserves the specific array type:

Also, IMHO, @([int[]] (1, 2)).GetType().Name [returning Int32[]] is a bug (over-optimization; it returns Object[] in v2)


As of Windows PowerShell v5.1 / PowerShell Core v6.0.0-beta.4, @() unexpectedly wraps arrays / collections instantiated directly as .NET types with the New-Object cmdlet in an outer, single-element array; in other words: it doesn't recognize that the results already are array-valued:

> @(New-Object 'Object[]' 2).Count; @(New-Object 'Object[]' 2)[0].Count
1  # !! The array was unexpectedly wrapped in an outer single-item array.
2  # !! Element [0] contains the original array.

> @(New-Object 'System.Collections.ArrayList').Count; @(New-Object 'System.Collections.ArrayList')[0].Count
1  # !! The array list was unexpectedly wrapped in an outer single-item array.
0  # !! Element [0] contains the original (empty) array list.

To contrast the surprising New-Object behavior above with commands that should be equivalent, but work as expected:

> @((New-Object 'Object[]' 2)).Count
2 # OK - !! Simply enclosing the New-Object call in (...) made the difference.

> @([int[]] (1, 2)).Count
2 # OK - using a cast in lieu of New-Object

> @([System.Collections.ArrayList]::new()).Count
0 # OK - using the static ::new() method in lieu of New-Object

Environment data

PowerShell Core v6.0.0-beta.4 on macOS 10.12.5
PowerShell Core v6.0.0-beta.4 on Ubuntu 16.04.2 LTS
PowerShell Core v6.0.0-beta.4 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.The question is answered.WG-Languageparser, language semanticsparser, language semantics

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.