- Null coalescing operator
-
The null coalescing operator (or Logical Defined Or Operator) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#,[1] and Perl as of version 5.10.[2]
In Scheme, "boolean false" and "null" are represented by the same value, written as #f. Furthermore, #f is the only value in Scheme that "counts as false" in a boolean context. Unlike some other languages, values like 0, the empty string, and the empty list, all count as true. The boolean 'or' operation, written as (or x y), returns x if it is not false, otherwise it returns y. Thus, in Scheme, there is no need for a separate "null coalescing operator", because the or operator serves that purpose.
Contents
Conditional assignment
C#
In C#, the null coalescing operator is
??
. It is most often used to simplify null expressions as follows:possiblyNullValue ?? valueIfNull
For example, if we wish to implement some C# code to give a page a default title if none is present, we may use the following statement:
string pageTitle = suppliedTitle ?? "Default Title";
instead of the more verbose
string pageTitle = (suppliedTitle == null) ? "Default Title" : suppliedTitle;
or
string pageTitle; if (suppliedTitle == null) pageTitle = "Default Title"; else pageTitle = suppliedTitle;
The three forms are logically equivalent.
Perl
In Perl (starting with version 5.10), the operator is
//
and the equivalent Perl code is:$possibly_null_value // $value_if_null
The possibly_null_value is evaluated as null or not-null (or, in Perl, undefined or defined). On the basis of the evaluation, the expression returns either value_if_null when possibly_null_value is null, or possibly_null_value otherwise. This is similar to the way ternary operators (
?:
statements) work in languages that support them. The above Perl code is equivalent to the use of the ternary operator below:defined($possibly_null_value) ? $possibly_null_value : $value_if_null
This operator's most common usage is to minimize the amount of code used for a simple null check.
In Perl, the simpler expression below will often produce the same result, since the logical
||
will evaluate to the first true expression:$possibly_null_value || $value_if_null
However, this does not distinguish between an undefined value, and other "false" values such as 0 and the empty string, so can only be used safely if you know that $possibly_null_value, if defined, will be a "true" value.
PL/SQL
In Oracle's PL/SQL, the NVL() function provides the same outcome:
NVL(possibly_null_value, 'value if null');
In SQL Server/Transact-SQL there is the ISNULL function the follows the same prototype pattern:
ISNULL(possibly_null_value, 'value if null');
Attention should be taken for not confusing ISNULL with IS NULL - the last serves to evaluate whether some contents is defined to be NULL or not.
See also
References
Categories:- Conditional constructs
- Operators (programming)
Wikimedia Foundation. 2010.