Skip to content
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

Closed
eosfor opened this issue May 3, 2018 · 5 comments
Closed

Strange behavior of the function #6814

eosfor opened this issue May 3, 2018 · 5 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.

Comments

@eosfor
Copy link

eosfor commented May 3, 2018

Function returns nothing but should return an empty list

image

image

@mklement0
Copy link
Contributor

By default, function outputs are enumerated if they are collections, and enumerating an empty list yields "nothing" (more precisely, a special "null collection" value, [System.Management.Automation.Internal.AutomationNull]::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 List`1, proving that the list was returned.
The aux. wrapper array was enumerated, outputting just the list.

Do note that the conceptually cleaner approach would be to use Write-Output -NoEnumerate, but Write-Output is currently broken - see #5955

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 [psobject[]] array in lieu of the list.

@eosfor
Copy link
Author

eosfor commented May 3, 2018

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.

@mklement0
Copy link
Contributor

mklement0 commented May 3, 2018

@eosfor:

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 List`1; that is, no enumeration was performed.

@iSazonov iSazonov added Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered. labels May 3, 2018
@eosfor eosfor closed this as completed May 3, 2018
@eosfor
Copy link
Author

eosfor commented May 3, 2018

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.

@mklement0
Copy link
Contributor

@eosfor:

Yes, the wrapper-array solution is obscure (though conveniently short).

Note that #5955 describes a regression bug. That is, Write-Output -NoEnumerate has always worked (and has always been the conceptually cleaner approach) - and still does in Windows PowerShell v5.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.
Projects
None yet
Development

No branches or pull requests

3 participants