Null coalescing operator

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

  1. ^ ?? Operator (C# Reference)
  2. ^ // Operator (Perl Reference)

Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Null Object pattern — Null object redirects here. For the concept in category theory, see Initial object. In object oriented computer programming, a Null Object is an object with defined neutral ( null ) behavior. The Null Object design pattern describes the uses of… …   Wikipedia

  • ?? Operator — The ?? operator, sometimes called the Coalescing Operator, is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, most notably C#.Conditional assignment?? is most frequently used to… …   Wikipedia

  • Null (SQL) — The Greek lowercase omega (ω) character is used to represent Null in database theory. Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database. Introduced by the creator of the… …   Wikipedia

  • Null function — Not to be confused with identity function or empty function. In computer science, a null function (or null operator) is subroutine that returns no data values and leaves the program state unchanged. When it is part of the instruction set of… …   Wikipedia

  • Null character — For other uses, see Null symbol. The null character (also null terminator), abbreviated NUL, is a control character with the value zero.[1] [2] It is present in many character sets, including ISO/IEC 646 (or ASCII), the C0 control code, the… …   Wikipedia

  • /dev/null — In Unix like operating systems, /dev/null or the null device is a special file that discards all data written to it (but reports that the write operation succeeded) and provides no data to any process that reads from it (yielding EOF immediately) …   Wikipedia

  • ?: — In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator or inline if (iif). It originally comes from… …   Wikipedia

  • Pointer (computing) — This article is about the programming data type. For the input interface (for example a computer mouse), see Pointing device. Pointer a pointing to the memory address associated with variable b. Note that in this particular diagram, the computing …   Wikipedia

  • Nullable type — In programming, nullable types are a feature of some statically typed programming languages which allows a data type to be set to the special value NULL instead of their common range of possible values. For value types or built in data types like …   Wikipedia

  • Empty string — In computer science and formal language theory, the empty string (or null string)[1] is the unique string of length zero. It is denoted with λ or sometimes Λ or ε. The empty string is distinct from a null reference in that in an object oriented… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”