Skip to content

[suggested trap] Weird null-ness #2

@SergeyTeplyakov

Description

@SergeyTeplyakov

Maybe this is already in your list, but just in case.

Consider following two cases:

$tmp = (1..5 | where {$_ -gt 6} | select -First 1)
$list1 = @(, $tmp)
$list1.Length # 0

$list2 = @(, $(if($tmp -eq $null) {$null} else {$tmp}))
$list2.Length # 1

To create an array with one element from piped expression additional magic step is required.

In first case list1 is an empty array, but in the second case it has one element equals to $null.

This is really strange for me, because for me expression like $(if($tmp -eq $null) {$null} else {$tmp}) is equivalent to $tmp.

P.S. I've found this issue trying to create closures in PowerShell. Because closures captures outer variable by value you have to wrap captured variable in array or another mutable instance. But that's another story.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions