Skip to content

Invoke-NullCoalescing lies #93

@wekempf

Description

@wekempf

I'm not sure why this command is exported publicly? It has nothing to do with Git, and as such I'd prefer it to not be exported, honestly. If you are going to export it, though, it should do what the name says it does.

PS> Invoke-NullCoalescing '' 'oops'
oops
PS> Invoke-NullCoalescing $false 'oops'
oops
PS> Invoke-NullCoalescing 0 'oops'
oops

I'm sure you can think of many other ways to get the wrong result here. However, there is one more example that you may consider correct, but I find to be very confusing.

PS> Invoke-NullCoalescing { } 'oops'
oops

Invoking the ScriptBlocks may be what you need, but it seems very surprising to me. If I were to implement this it would have been simply:

function Invoke-NullCoalescing {
   $args | Where-Object { $_ -ne $null } | Select-Object -First 1
}

To include the script block processing, something like:

function Invoke-NullCoalescing([switch]$ProcessScriptBlocks) {
   $args | ForEach-Object {
        if ($_ -is [ScriptBlock] -and $ProcessScriptBlocks) {
            & $_
        } else {
            $_
        }
    } | Where-Object { $_ -ne $null } | Select-Object -First 1
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions