Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defaults for null values

Tags:

powershell

Working on a Powershell script I had several places where I wanted A unless it was null, else B. Essentially the ?? operator in C#. I ended up writing the function shown below, but I can't help but think there is a built-in way to do this.

Is there a better, built-in, way?

function Get-ValueOrDefault()
{
    foreach ($value in $args)
    {
        if ($value -ne $null) { return $value }
    }
}

I think this works better:

function Get-ValueOrDefault() { $args | select -first 1 }
like image 528
OldFart Avatar asked Mar 05 '10 21:03

OldFart


People also ask

What is default NULL in SQL?

By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

What is the default sorting order in SQL with NULL values?

If you specify the ORDER BY clause, NULL values by default are ordered as less than values that are not NULL. Using the ASC order, a NULL value comes before any non-NULL value; using DESC order, the NULL comes last.

What is default NULL in MySQL?

If a data type specification includes no explicit DEFAULT value, MySQL determines the default value as follows: If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause. If the column cannot take NULL as a value, MySQL defines the column with no explicit DEFAULT clause.

Which has a default value of NULL Java?

Reference Variable value: Any reference variable in Java has a default value null.


1 Answers

This is what we provide in the PowerShell Community Extensions:

<#
.SYNOPSIS
    Similar to the C# ?? operator e.g. name = value ?? String.Empty
.DESCRIPTION
    Similar to the C# ?? operator e.g. name = value ?? String.Empty;
    where value would be a Nullable&lt;T&gt; in C#.  Even though PowerShell
    doesn't support nullables yet we can approximate this behavior.
    In the example below, $LogDir will be assigned the value of $env:LogDir
    if it exists and it's not null, otherwise it get's assigned the
    result of the second script block (C:\Windows\System32\LogFiles).
    This behavior is also analogous to Korn shell assignments of this form:
    LogDir = ${$LogDir:-$WinDir/System32/LogFiles}
.PARAMETER PrimaryExpr
    The condition that determines whether the TrueBlock scriptblock is used or the FalseBlock
    is used.
.PARAMETER AlternateExpr
    This block gets evaluated and its contents are returned from the function if the Conditon
    scriptblock evaluates to $true.
.EXAMPLE
    C:\PS> $LogDir = ?? {$env:LogDir} {"$env:windir\System32\LogFiles"}
    $LogDir is set to the value of $env:LogDir unless it doesn't exist, in which case it 
    will then default to "$env:windir\System32\LogFiles".
#>
filter Invoke-NullCoalescing {
    param([scriptblock]$PrimaryExpr   = $(throw "Parameter '-primaryExpr' (position 1) required"), 
          [scriptblock]$AlternateExpr = $(throw "Parameter '-alternateExpr' (position 2) required"))

    if ($primaryExpr -ne $null) {
        $result = &$primaryExpr
        if ($result -ne $null -and "$result" -ne '') {
            $result
        }
        else {
            &$alternateExpr
        }
    }
    else {
        &$alternateExpr
    }
}

New-Alias ?? Invoke-NullCoalescing

PS> ?? {$xyzzy} {"empty"}
empty

PS> ?? {$psversiontable} {"empty"}

Name                           Value
----                           -----
CLRVersion                     2.0.50727.4927
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1
like image 51
Keith Hill Avatar answered Sep 30 '22 21:09

Keith Hill