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)
tl; dr:
This issue is written based on the following, currently unfulfilled expectation:
New-Objectcall 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):
On the tangentially related issue that
@()preserves the specific array type:As of Windows PowerShell v5.1 / PowerShell Core v6.0.0-beta.4,
@()unexpectedly wraps arrays / collections instantiated directly as .NET types with theNew-Objectcmdlet in an outer, single-element array; in other words: it doesn't recognize that the results already are array-valued:To contrast the surprising
New-Objectbehavior above with commands that should be equivalent, but work as expected:Environment data