You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object #4257
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.WG-Languageparser, language semantics
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)
changed the title [-]Surprising behavior of @() (array subexpression operator) with null collections and arrays/collections created with New-Object[/-][+]Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object[/+]on Jul 14, 2017
. {
some statements here
} |ForEach-Object-Begin { $Temp= [Collections.ArrayList]::new() } -Process { [void]$Temp.Add($_) } -End { $Result=$Temp.ToArray() }
So @() invoke provided statements, collect all the pipeline output and create array from them. Notice: it absolutely does not care what kind of objects (scalar, array or some other collection) you write into pipeline, and that is where great power of @() is.
Note special case: @(<array literal>). I think that in this case @() is eliminated as optimization, but that leads to incorrect, IMHO, result, when <array literal> is typed. @([int[]] (1, 2)).GetType().Name return Int32[] instead of Object[].
How is that useful? It is useful when you work with arrays of arrays (jagged arrays in .NET terms). Suppose you have following command:
$Result=Get-Content File -ReadCount 3
If you have 9 lines in File, then result will be [[1, 2, 3], [4, 5, 6], [7, 8, 9]] (array of arrays). But if have only 3 lines, then result will be [1, 2, 3], but not [[1, 2, 3]]. What if you always want to have array of arrays, regardless of number of lines in File? Simple solution:
$Result=@(Get-Content File -ReadCount 3)
Other example: suppose you want to filter array of rows (arrays) by some condition, and then find how many are actually satisfy condition.
The same apply on any other transformation of array of rows, for example sorting. @() make this cases work especially because it wrap single array wrote into pipeline into another single element array, which other methods, like casting to [Array], did not do.
So, how does @(New-Object 'Object[]' 2) different from @($Array | Where-Object { $_[1]%2 }) from above? Right answer: them does not different, both write array into pipeline as single object, and it got wrapped by @() operator, because it is what that operator do.
One thing everyone should understand about @() is that it always treat its content as statement, not as expression. That means that standard collection unrolling behavior apply, when expression supplied as first (or only) element of pipeline.
$a= [int[]](1,2)
@(
$a; # write two objects into pipeline
).Count # 2$AutomationNull= [System.Management.Automation.Internal.AutomationNull]::Value
@(
$AutomationNull; # write zero objects into pipeline, do not work same way in v2
).Count # 0@(
'Scalar'; # write single object into pipeline
).Count # 1functionWriteArrayAsSingleObject { ,(1..5) } # nothing special in New-Object, after all@(
WriteArrayAsSingleObject; # write single object into pipeline
).Count # 1@(
(WriteArrayAsSingleObject); # write five objects into pipeline, do not work same way in v2
).Count # 5
PetSerAl: Thanks for the background info and the helpful examples.
First, let me say that my example is obviously contrived and this issue may rarely, if ever, arise in the real world - of course, if you're explicitly constructing an array / collection, wrapping it in @() is pointless.
In the following examples I'll use something other than an array for illustration, namely an empty[System.Collections.ArrayList] instance.
If I understand you correctly, you're saying that:
The equivalent of this cmdlet-based statement:
# Yields an [object[]] array whose 1 item is the empty array list.@(New-Object System.Collections.ArrayList)
is the following expression-based statement:
# To get the same result with an expression, the [System.Collections.ArrayList]::new() # call must be wrapped in an array(!).@(, [System.Collections.ArrayList]::new())
This equivalence in itself is puzzling - why wouldn't the equivalent be just @([System.Collections.ArrayList]::new()), without the wrapper array?
It seems to me that there's no good conceptual reason for New-Object to wrap the instance created in an outer, single-element array when writing to the pipeline - but, if I understand you correctly, doing so is a technical necessity, because the collection instance would otherwise invariably be converted to System.Object[] on output, correct?
Here's another example of where the distinction is puzzling, at least to those not intimately familiar with the inner workings of PowerShell, and taking that perspective (as someone who has some understanding of them) was the reason for creating this issue:
# Yields *1*, because the *wrapped* empty array list is sent through the pipeline.New-Object System.Collections.ArrayList |Measure-Object|% Count
# Yields *0*, because the empty array list itself is enumerated:
[System.Collections.ArrayList]::new() |Measure-Object|% Count
The fact that (...) causes unwrapping of pipeline output is also far from obvious:
# Now yields *0*, because the (...) forces unwrapping.
(New-Object System.Collections.ArrayList) |Measure-Object|% Count
Incidentally, that makes inspecting the true output generated by New-Object System.Collections.ArrayList all but impossible:
# -InputObject must be used to inspect collections (using the pipeline would unwrap),# but the unavoidable use of (...) unwraps too.Get-Member-InputObject (New-Object System.Collections.ArrayList)
# Storing in a variable doesn't help, because the assignment unwraps too.$al=New-Object System.Collections.ArrayList
Get-Member-InputObject $al
Given that this seemingly comes down to the fundamentals of pipeline behavior, there's probably nothing that can be done - except perhaps consider including the findings in a collection of yet-to-be-created advanced help topics.
Unless someone thinks that an actionable change is warranted here, I'll close this issue soon.
Note special case: @(<array literal>). I think that in this case @() is eliminated as optimization, but that leads to incorrect, IMHO, result, when <array literal> is typed. @([int[]] (1, 2)).GetType().Name return Int32[] instead of Object[].
I think what you mean is an expression that creates an array (PowerShell has no array literals, only the , operator for array construction, and here a cast (class instantiation) is involved as well).
I have no real opinion, but why do you think it is incorrect?
Because @() should consistently create System.Object[] arrays?
What's the harm in keeping the specific type?
Something like the following still works, for instance (and then yields [System.Object[]]):
@([int[]] (1,2)) +'three'
As a minor consideration, arrays typed as value types perform better (though (shallow) cloning of the array still happens).
# Yields an [object[]] array whose 1 item is the empty array list.@(New-Object System.Collections.ArrayList)
is the following expression-based statement:
# To get the same result with an expression, the [System.Collections.ArrayList]::new()# call must be wrapped in an array(!).@(, [System.Collections.ArrayList]::new())
This equivalence in itself is puzzling - why wouldn't the equivalent be just @([System.Collections.ArrayList]::new()), without the wrapper array?
Because PowerShell only unwrap result of expressions, but not result of commands:
So, when you use an expression, then you need something to prevent unwrapping.
It seems to me that there's no good conceptual reason for New-Object to wrap the instance created in an outer, single-element array when writing to the pipeline - but, if I understand you correctly, doing so is a technical necessity, because the collection instance would otherwise invariably be converted to System.Object[] on output, correct?
PowerShell do not automatically unwrap collections for compiled cmdlets, unless so was explicitly asked. You can see that, if you use $PSCmdlet.WriteObject in advanced function, because it is what cmdlets use to write objects into pipeline:
So, New-Object simply does not ask for unwrapping behavior in the first palace. It does not wrap ArrayList into something, because it just not necessary for cmdlets.
Here's another example of where the distinction is puzzling, at least to those not intimately familiar with the inner workings of PowerShell, and taking that perspective (as someone who has some understanding of them) was the reason for creating this issue:
# Yields *1*, because the *wrapped* empty array list is sent through the pipeline.New-Object System.Collections.ArrayList |Measure-Object|% Count
# Yields *0*, because the empty array list itself is enumerated:
[System.Collections.ArrayList]::new() |Measure-Object|% Count
Here’s how array literals are defined in PowerShell: They’re not. There’s no array literal notation in PowerShell. […] instead of having array literals, there’s a set of operations that create collections as needed.
PetSerAl is right, PowerShell unravels collection results from expressions, but not commands.
Commands themselves decide whether the result writen to pipe should be unravelled or not (Cmdlet.WriteObject(object) and Cmdlet.WriteObject(object, enumerateCollection) for C# and Write-Output and Write-Output -NoEnumerate for script).
As for the quote, here is more context on it:
Most programming languages have some kind of array literal notation similar to the PowerShell hash literal notation (@{...}), where there's a beginning character sequence followed by a list of values, followed by a closing character sequence. Here’s how array literals are defined in PowerShell: They’re not. There’s no array literal notation in PowerShell. […] instead of having array literals, there’s a set of operations that create collections as needed.
I think the point is that you don't need a notation (beginning char sequence + closing char sequence) to create an array in PowerShell, for example, 1,2,3,4 is defining an array. But PowerShell does have ArrayLiteralRule in parser and ArrayLiteralAst.
The situation changes with a cast
The ArrayLiteralAst is embedded in the ConvertExpressionAst:
Given the potentially problematic behavior of something like @([int[]] (1, 2)) (the only case where @() doesn't output an [object[]] array), is it worth creating a new issue for that?
It's hard to imagine that anyone would have relied on that behavior, and eliminating it makes for a more predictable environment.
Here is the code that handles this special case. In my understanding, this happens only in one of the following two conditions:
the expression is a ConvertExpressionAst or a ParenExpression that wraps a ConvertExpressionAst, and the convert-to type is an array.
the expression is an ArrayLiteralAst or a ParenExpression that wraps an ArrayLiteralAst.
In the first case, since the conversion is explicitly specified, it's very unlikely the resulting array will again be used as object[]. In the second case, the resulting array from ArrayLiteralAst is already object[] so it doesn't matter. Plus, altering this behavior would be a breaking change. Given the above, I prefer to not make a behavior change. But feel free to notify the PowerShell-Committee if you want a further discussion.
I can't imagine much existing code breaking, but, conversely, I can't imagine many people running into a problem with the current behavior in real-world scenarios, so, personally, I'm happy to leave it at that; perhaps PetSerAl feels differently.
Now $b is not copy of $a but is $a. Also if current behavior will be kept, is not ArrayExpressionAst.StaticType should be adjusted to match current behavior?
Yes, $b and $a point to the same array in this case. But I don't think the array expression in powershell language is defined to always return a new array object.
Is not ArrayExpressionAst.StaticType should be adjusted to match current behavior?
Good catch. It is inconsistent and should be fixed. But IMHO, the fix should be to change ArrayExpressionAst.StaticType to always return System.Array (also a breaking change :)). StaticType is by definition not accurate, because sometimes the actual type can be known only at runtime, for example, BinaryExpressionAst.StaticType returns System.Object when it's -bor, and the actual type may vary at runtime.
Dongbo Wang (@daxian-dbw) But where I can see that definition of array expression? Before now I always expect it to return new array object each time. I myself use $b = @($a) (without cast although) as array copy operator, and I really do not like if it stop copying array at some point in the future.
You can find the Windows PowerShell Langauge Specification Version 3.0here. It hasn't been updated for a while, so new language features like DSC or PowerShell Class are not in it, but I believe the content in the specification should still apply to PowerShell 6.0.
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.WG-Languageparser, language semantics
Activity
[-]Surprising behavior of @() (array subexpression operator) with null collections and arrays/collections created with New-Object[/-][+]Surprising behavior of @() (array subexpression operator) with arrays/collections created with New-Object[/+]PetSerAl commentedon Jul 14, 2017
Let me describe how I understand how
@()work.Suppose you have this:
It is more or less equivalent to:
So
@()invoke provided statements, collect all the pipeline output and create array from them. Notice: it absolutely does not care what kind of objects (scalar, array or some other collection) you write into pipeline, and that is where great power of@()is.Note special case:
@(<array literal>). I think that in this case@()is eliminated as optimization, but that leads to incorrect, IMHO, result, when<array literal>is typed.@([int[]] (1, 2)).GetType().NamereturnInt32[]instead ofObject[].How is that useful? It is useful when you work with arrays of arrays (jagged arrays in .NET terms). Suppose you have following command:
If you have 9 lines in
File, then result will be[[1, 2, 3], [4, 5, 6], [7, 8, 9]](array of arrays). But if have only 3 lines, then result will be[1, 2, 3], but not[[1, 2, 3]]. What if you always want to have array of arrays, regardless of number of lines inFile? Simple solution:Other example: suppose you want to filter array of rows (arrays) by some condition, and then find how many are actually satisfy condition.
That is how to do it right:
The same apply on any other transformation of array of rows, for example sorting.
@()make this cases work especially because it wrap single array wrote into pipeline into another single element array, which other methods, like casting to[Array], did not do.So, how does
@(New-Object 'Object[]' 2)different from@($Array | Where-Object { $_[1]%2 })from above? Right answer: them does not different, both write array into pipeline as single object, and it got wrapped by@()operator, because it is what that operator do.PetSerAl commentedon Jul 14, 2017
One thing everyone should understand about
@()is that it always treat its content as statement, not as expression. That means that standard collection unrolling behavior apply, when expression supplied as first (or only) element of pipeline.mklement0 commentedon Jul 16, 2017
PetSerAl: Thanks for the background info and the helpful examples.
First, let me say that my example is obviously contrived and this issue may rarely, if ever, arise in the real world - of course, if you're explicitly constructing an array / collection, wrapping it in
@()is pointless.In the following examples I'll use something other than an array for illustration, namely an empty
[System.Collections.ArrayList]instance.If I understand you correctly, you're saying that:
The equivalent of this cmdlet-based statement:
is the following expression-based statement:
This equivalence in itself is puzzling - why wouldn't the equivalent be just
@([System.Collections.ArrayList]::new()), without the wrapper array?It seems to me that there's no good conceptual reason for
New-Objectto wrap the instance created in an outer, single-element array when writing to the pipeline - but, if I understand you correctly, doing so is a technical necessity, because the collection instance would otherwise invariably be converted toSystem.Object[]on output, correct?Here's another example of where the distinction is puzzling, at least to those not intimately familiar with the inner workings of PowerShell, and taking that perspective (as someone who has some understanding of them) was the reason for creating this issue:
The fact that
(...)causes unwrapping of pipeline output is also far from obvious:Incidentally, that makes inspecting the true output generated by
New-Object System.Collections.ArrayListall but impossible:Given that this seemingly comes down to the fundamentals of pipeline behavior, there's probably nothing that can be done - except perhaps consider including the findings in a collection of yet-to-be-created advanced help topics.
Unless someone thinks that an actionable change is warranted here, I'll close this issue soon.
mklement0 commentedon Jul 16, 2017
P.S.:
Re:
I think what you mean is an expression that creates an array (PowerShell has no array literals, only the
,operator for array construction, and here a cast (class instantiation) is involved as well).I have no real opinion, but why do you think it is incorrect?
Because
@()should consistently createSystem.Object[]arrays?What's the harm in keeping the specific type?
Something like the following still works, for instance (and then yields
[System.Object[]]):As a minor consideration, arrays typed as value types perform better (though (shallow) cloning of the array still happens).
mklement0 commentedon Jul 17, 2017
PetSerAl:
I think I finally wrapped my head around what you were saying, including why the type-preserving optimization may be problematic.
Thanks for your help.
I've updated my SO answer, which hopefully now contains a correct and comprehensive description of how
@()works.PetSerAl commentedon Jul 17, 2017
Because PowerShell only unwrap result of expressions, but not result of commands:
{ New-Object System.Collections.ArrayList }.Ast.EndBlock.Statements[0].PipelineElements[0].GetType().Name # CommandAst { [System.Collections.ArrayList]::new() }.Ast.EndBlock.Statements[0].PipelineElements[0].GetType().Name # CommandExpressionAstSo, when you use an expression, then you need something to prevent unwrapping.
PowerShell do not automatically unwrap collections for compiled cmdlets, unless so was explicitly asked. You can see that, if you use
$PSCmdlet.WriteObjectin advanced function, because it is what cmdlets use to write objects into pipeline:So,
New-Objectsimply does not ask for unwrapping behavior in the first palace. It does not wrapArrayListinto something, because it just not necessary for cmdlets.Same distinction: command vs. expression:
{ New-Object System.Collections.ArrayList | Measure-Object | % Count }.Ast.EndBlock.Statements[0].PipelineElements[0].GetType().Name { [System.Collections.ArrayList]::new() | Measure-Object | % Count }.Ast.EndBlock.Statements[0].PipelineElements[0].GetType().NameParenthesis
(...)do not cause unwrapping. Them transformcommandintoexpression:{ (New-Object System.Collections.ArrayList) | Measure-Object | % Count }.Ast.EndBlock.Statements[0].PipelineElements[0].GetType().NameAnd because that expression used as first element in pipeline, that cause unwrapping.
In my opinion true output of
New-Object System.Collections.ArrayListis anArrayListinstance, and both your commands show members of it.{ 1, 2, 3 }.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.GetType().NameYes, I think it should be always
System.Object[]. Also:{ @([int[]] (1, 2, 3)) }.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.StaticTypemklement0 commentedon Jul 17, 2017
PetSerAl:
That's all very illuminating and I appreciate that you took the time to explain it.
I have updated the SO answer yet again, and I'd appreciate it if you could take another look so I don't make any incorrect claims there.
Re array literals:
I guess it's just a matter of terminology - there is no disagreement in substance here.
I based my statement on this quote from PowerShell in Action, 2nd Edition (emphasis added):
P.S.: The situation changes with a cast:
daxian-dbw commentedon Jul 17, 2017
Great discussions!!!
PetSerAl is right, PowerShell unravels collection results from expressions, but not commands.
Commands themselves decide whether the result writen to pipe should be unravelled or not (
Cmdlet.WriteObject(object)andCmdlet.WriteObject(object, enumerateCollection)for C# andWrite-OutputandWrite-Output -NoEnumeratefor script).As for the quote, here is more context on it:
I think the point is that you don't need a notation (beginning char sequence + closing char sequence) to create an array in PowerShell, for example,
1,2,3,4is defining an array. But PowerShell does haveArrayLiteralRulein parser andArrayLiteralAst.The
ArrayLiteralAstis embedded in theConvertExpressionAst:{ [int[]] (1, 2, 3) }.Ast.EndBlock.Statements[0].PipelineElements[0].Expression.Child.Pipeline.PipelineElements[0 ].Expression.GetType().Name ArrayLiteralAstmklement0 commentedon Jul 17, 2017
Thanks, Dongbo Wang (@daxian-dbw), that's great to know.
Given the potentially problematic behavior of something like
@([int[]] (1, 2))(the only case where@()doesn't output an[object[]]array), is it worth creating a new issue for that?It's hard to imagine that anyone would have relied on that behavior, and eliminating it makes for a more predictable environment.
daxian-dbw commentedon Jul 18, 2017
Here is the code that handles this special case. In my understanding, this happens only in one of the following two conditions:
ConvertExpressionAstor a ParenExpression that wraps aConvertExpressionAst, and the convert-to type is an array.ArrayLiteralAstor a ParenExpression that wraps anArrayLiteralAst.In the first case, since the conversion is explicitly specified, it's very unlikely the resulting array will again be used as
object[]. In the second case, the resulting array fromArrayLiteralAstis alreadyobject[]so it doesn't matter. Plus, altering this behavior would be a breaking change. Given the above, I prefer to not make a behavior change. But feel free to notify the PowerShell-Committee if you want a further discussion.mklement0 commentedon Jul 18, 2017
Thanks, Dongbo Wang (@daxian-dbw).
I can't imagine much existing code breaking, but, conversely, I can't imagine many people running into a problem with the current behavior in real-world scenarios, so, personally, I'm happy to leave it at that; perhaps PetSerAl feels differently.
PetSerAl commentedon Jul 18, 2017
Dongbo Wang (@daxian-dbw), how about this:
Now
$bis not copy of$abut is$a. Also if current behavior will be kept, is notArrayExpressionAst.StaticTypeshould be adjusted to match current behavior?mklement0 commentedon Jul 18, 2017
PetSerAl:
Excellent points - I now agree that this should be fixed, so I've created #4280.
daxian-dbw commentedon Jul 18, 2017
Yes,
$band$apoint to the same array in this case. But I don't think the array expression in powershell language is defined to always return a new array object.Good catch. It is inconsistent and should be fixed. But IMHO, the fix should be to change
ArrayExpressionAst.StaticTypeto always returnSystem.Array(also a breaking change :)).StaticTypeis by definition not accurate, because sometimes the actual type can be known only at runtime, for example,BinaryExpressionAst.StaticTypereturnsSystem.Objectwhen it's-bor, and the actual type may vary at runtime.PetSerAl commentedon Jul 18, 2017
Dongbo Wang (@daxian-dbw) But where I can see that definition of array expression? Before now I always expect it to return new array object each time. I myself use
$b = @($a)(without cast although) as array copy operator, and I really do not like if it stop copying array at some point in the future.daxian-dbw commentedon Jul 18, 2017
You can find the
Windows PowerShell Langauge Specification Version 3.0here. It hasn't been updated for a while, so new language features like DSC or PowerShell Class are not in it, but I believe the content in the specification should still apply to PowerShell 6.0.