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
Strange behavior of the function #6814
Comments
By default, function outputs are enumerated if they are collections, and enumerating an empty list yields "nothing" (more precisely, a special "null collection" value, In order to output your list as-is, as a single object, wrap it in an auxiliary array: function foo {
$ret = [System.Collections.Generic.List[long]]::new()
, $ret # construct a single-element wrapper array around the list
}
$k = foo
$k.GetType().Name The above yields Do note that the conceptually cleaner approach would be to use function foo {
$ret = [System.Collections.Generic.List[long]]::new()
# This SHOULD work, but is currently BROKEN - see #5955
Write-Output -NoEnumerate $ret
} As of PowerShell Core v6.0.2, the above yields an empty |
Hm, ok, seems reasonable. But to me using either one is kind of unexpected. I'd prefer it to return a value, even it is is an empty collection. |
Your being surprised is understandable if you're used to function semantics from other languages, but in PowerShell, functions integrate tightly with the pipeline, a salient feature of which is implicit enumeration of collections. Thus, unlike in other languages, you have to opt out of this automatic enumeration. Note that expressions, by contrast, are not automatically enumerated: $k = [System.Collections.Generic.List[long]]::new()
$k.GetType().Name The above yields |
Yes, i agree. I know about this feature. However when you create a collection and expect it to be returned as is, wrapping it into a single-value array is, kind of, counterintuitive, IMHO. I'd say, the solution you proposed in #5955 is a good idea. |
Function returns nothing but should return an empty list
The text was updated successfully, but these errors were encountered: