if-non-null (macro)
Package: CURL.LANGUAGE.COMPILER

Signature

{if-non-null [var[:type] =] expr [, ...] then
  if-body
[elseif cond then elseif-body] ...
[else else-body]
}

Special version of if for null comparison.

Description

This expression is similar to the standard if expression, except instead of evaluating a boolean condition, it compares the value of an expression expr with null and can set a local constant variable to that value. If expr is non-null, then the if-body will be evaluated, otherwise any elseif clauses and the final else clause will be evaluated if present, just as in the standard if expression. Provided that an else clause is provided, the if-non-null expression may produce a value based on which clause is selected, just as in the standard if expression. For example:

let str-to-use:String =
    {if-non-null str = {get-possibly-null-string} then
        str
     else
        "default string"
    }


If a var is specified, then the value of expr will be assigned to a constant variable with that name within the if-body, when expr is non-null. The type of var is type, if specified, and otherwise defaults to the non-null variant of the type of expr. Unlike let assignments, expr may be identical to var, as in:

{define-proc public {use-string str:String}:void 
    || ...
}
{define-proc {use-string-if-exists str:#String}:void
    {if-non-null str = str then
        {use-string str}
    }
}


In fact, if expr is a simple identifier, it is treated just like this, so the previous example could have been written:

{if-non-null str then
    {use-string str}
}


Multiple comma-separated conditions may be specified in this fashion (this ability was introduced in the 6.0 version of the API), in which case each successive expression will only be evaluated if the previous one is non-null. The if-body will only be evaluated if all of the conditions are non-null, otherwise control will fall through to any elseif or else clauses. For example:

let table:#HashTable

|| ...

{if-non-null table, foo = {table.get-if-exists "foo"} then
    {use-foo foo}
 else
    {error "no foo"}
}