I only just noticed that this is valid PowerShell code:
PS> $first, $rest = @(1, 2, 3)
This statement puts the first item in the array in $first
and the remaining items in $rest
.
PS> $first
1
PS> $rest
2
3
It even works for an arbitrary number of variables, pushing the current head into the next variable and the tail into the last one. You can try that for yourself.
PS> $first, $second, $rest = @(1, 2, 3, 4)
It seems to assign a $null
value if there aren't enough heads or tails to put into one of the variables. Even in the case of the $rest
(I'd rather have seen an empty array, but whatever).
PS> $first, $second, $rest = @(1)
PS> $first
1
PS> $second
PS> $second -eq $null
True
PS> $rest
PS> $rest -eq $null
True
PS> $rest -eq @()
False
The problem, and my question, is that I don't see this documented anywhere! I'm trying to find out when this was supported. Exactly how it's implemented. If it works for any other types.
I checked about_Assignment
, about_Arrays
, and about_Splatting
, without any luck.