Skip to content

Returning nothing (Automation.Null) from an [object]-typed custom-class method breaks #7128

Closed

Description

Steps to reproduce

class Foo { [object] Bar() { return & {} } }; (new Foo).Bar()

Note that & {} outputs "nothing", i.e., the "null collection" [System.Management.Automation.Internal.AutomationNull]::Value

Expected behavior

Given that PowerShell allows casting any value to [object] - including $null and [System.Management.Automation.Internal.AutomationNull]::Value, the operation should succeed and the [System.Management.Automation.Internal.AutomationNull]::Value) should be passed through.

Bruce Payette (@BrucePay)'s take on the desired behavior differs, however (taken from this SO answer of his):

We should still generate an error but it should be "non-void methods must return a value" or some such

Actual behavior

The following error occurs:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: Index
...

An analysis of the cause, courtesy of Bruce Payette (@BrucePay)'s previously linked answer:

[...] the code that invokes the scriptblock expects there to be an object in the result List i.e. result.Count == 1. There are no objects in the pipeline case so you get an index-out-of-range error. [...]
BTW - the code in question is here.

Environment data

PowerShell Core v6.1.0-preview.2 on macOS 10.13.4
PowerShell Core v6.1.0-preview.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)

Activity

  1. added
    Issue-Bug
    WG-Engine
    on Jun 21, 2018
  2. BrucePay commented on Jun 21, 2018

    @BrucePay
    Collaborator

    Rather than generating an error which wouldn't have been very useful, I changed the code to return default(T).

  3. mklement0 commented on Jun 22, 2018

    @mklement0
    ContributorAuthor

    Thanks, Bruce Payette (@BrucePay), that makes sense.

    To spell it out, in the case of [object] - and any reference type - this will default to $null - see https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/default-value-expressions for the full story.

  4. winperec commented on Jun 26, 2018

    @winperec

    Thanks Bruce Payette (@BrucePay)
    The one thing I don't like every time I should check for $null before iterating(in case if my method should return array) because:

    $null | Foreach-Object { $_.someprop.prop = "some value" } # throws exception;
    
    &{} | Foreach-Object { $_.someprops.prop } # no throw because no iterating;
    

    so it means that after each call which return enumerable. I should check like:

    class Foo { [object] Bar() { return $somedata | Where-Object { <# filter data #> } };
    
    $myarray = (new Foo).Bar()
    if($myarray){
        $myarray | Foreach-Object { <# process data #> }
    }
    

    but it would be great to write just code in pipeline:
    $myarray = (new Foo).Bar() | Foreach-Object { <# process data #> }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-FixedWG-Engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions