PeterVandivier — 9/24/25, 12:31 PM

why does @(1,2)[$null] fail where @(1,2)[[int]$null] succeeds?

A PowerShell behavioural quick caught my attention debugging an little for-loop and prompted me to ask the above in PowerShell discord.

Usually, $null coerces intuitively in PowerShell…

> "Hello, $null." # empty string
# Hello, .

> 1 + $null # zero
# 1

…and provides a helpful message if it doesn’t…

> [timespan]$null                        
InvalidArgument: Cannot convert null to type "System.TimeSpan".

Why then (I asked…) is explicit typing needed in the index operator?

This is a throwaway question. I don’t need to know the answer. It’s one of the ten thousand language quirks you pick up on using the same tools for over a decade. You type these things into Twitter or Reddit or Quora or IRC as much in the hope of getting an answer as to just complain about it.

denvercoder9 knows what I’m talking about

…but something changed in the past decade… PowerShell went open source. You can look at the code now.

Now to be clear - FOSS software isn’t a documentation panacea. You still need people who know the code base and have a working knowledge of it to parse your question. Why only very recently I asked someone to write me a grep statement to get an answer out of a FOSS codebase I had already been digging in.

I go to the PowerShell discord precisely because it’s chockablock full of people who know the language and the code base and where to look. This is what it means to be in a community of practice and it’s deeply facilitated by FOSS principles.

I love deeply that I not only got an answer in minutes… but that it was given with receipts. That is - a link to the actual code comments in source.

Some times we do get to have nice things.


…oh yea… the reason for the language thing is “By Design” 🤪…

seeminglyscience — 9/24/25, 1:07 PM

it’s assumed to be significantly more likely to be a design time error rather than the author genuinely wanting 0 basically the get index binder itself explicitly throws when you pass null as an index for an array

https://github.com/PowerShell/PowerShell/blob/d8b1cc55332079d2be94cc266891c85e57d88c55/src/System.Management.Automation/engine/runtime/Binding/Binders.cs#L4028-L4038 relevant code with comments

// A null index is not allowed unless the index is one of the indices used while slicing, in which case we'll attempt
// the usual conversions from null to whatever the value being indexed supports.
// This is oddly inconsistent e.g.:
//     $a[$null] # error
//     $a[$null,$null] # no error, result is an empty array
// The rationale: V1/V2 did it, and when people are slicing, it's better to return some of the results than none.
if (indexes.Length == 1 && indexes[0].Value == null && _allowSlicing)
{
    return (errorSuggestion ??
            target.ThrowRuntimeError(indexes, BindingRestrictions.Empty, "NullArrayIndex", ParserStrings.NullArrayIndex)).WriteToDebugLog(this);
}

…which… come on… I love that you get to read the authors’ thoughts on the matter too…

so cool