Skip to content

In what conditions does operator [] return AutomationNull? #6436

Closed

Description

@alx9r

Chapter 7.1.4 of the PowerShell spec indicates that for arrays, strings, hashtables, and XML documents operator [] returns $null for non-existent indexes. The spec seems to be silent for other types, however. I expected that, for other types, operator [] would also return $null for non-existent indexes. It seems that for some combinations of types and indexes, however, operator [] returns AutomationNull. In what conditions does operator [] return AutomationNull?

Steps to reproduce

&{(1)[1]} |  % {'output from indexing [int] out-of bounds'}
&{(,1)[1]} | % {'output from indexing array out-of-bounds'}

Expected behavior

output from indexing [int] out-of bounds
output from indexing array out-of-bounds

Actual behavior

output from indexing array out-of-bounds

Environment data

> $PSVersionTable

Name                           Value                                            
----                           -----                                            
PSVersion                      6.0.0                                            
PSEdition                      Core                                             
GitCommitId                    v6.0.0                                           
OS                             Microsoft Windows 6.3.9600                       
Platform                       Win32NT                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                          
PSRemotingProtocolVersion      2.3                                              
SerializationVersion           1.1.0.1                                          
WSManStackVersion              3.0                                              

Activity

  1. alx9r commented on Mar 20, 2018

    @alx9r
    Author

    I think the logic is here, but I only have a guess at what that code does.

  2. alx9r commented on Mar 21, 2018

    @alx9r
    Author

    It looks like this is different behavior from selecting a non-existent property. Selecting a non-existent property seems to always output $null not AutomationNull:

    &{(1).NonExistent}  | % {'output from selecting non-existent property on [int]'}
    &{(,1).NonExistent} | % {'output from selecting non-existent property on [array]'}

    outputs

    output from selecting non-existent property on [int]
    output from selecting non-existent property on [array]
    
  3. mklement0 commented on Mar 22, 2018

    @mklement0
    Contributor

    Indeed, accessing a nonexistent property always returns $null, whether applied to a scalar or a collection.

    By contrast, indeed,

    • indexing a scalar out of bounds returns [System.Management.Automation.Internal.AutomationNull]::Value,
    • whereas indexing a collection out of bounds returns $null

    which is an interesting inconsistency (for which I have no explanation).

  4. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Apr 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions