Null coalescing operator
The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#[1] as of version 2.0,[2] PowerShell as of version 7.0.0,[3] Perl as of version 5.10,[4] Swift,[5] and PHP 7.0.0.[6] While its behavior differs between implementations, the null coalescing operator generally returns the result of its left-most operand if it exists and is not null, and otherwise returns the right-most operand. This behavior allows a default value to be defined for cases where a more specific value is not available.
In contrast to the ternary conditional if operator used as x ? x : y
, but like the binary Elvis operator used as x ?: y
, the null coalescing operator is a binary operator and thus evaluates its operands at most once, which is significant if the evaluation of x
has side-effects.
Examples by languages
Bourne-like Shells
In Bourne shell (and derivatives), "If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted":[7]
#supplied_title='supplied title' # Uncomment this line to use the supplied title title=${supplied_title:-'Default title'} echo "$title" # prints: Default title
C#
In C#, the null coalescing operator is ??
.
It is most often used to simplify expressions as follows:
possiblyNullValue ?? valueIfNull
For example, if one wishes to implement some C# code to give a page a default title if none is present, one may use the following statement:
string pageTitle = suppliedTitle ?? "Default Title";
instead of the more verbose
string pageTitle = (suppliedTitle != null) ? suppliedTitle : "Default Title";
or
string pageTitle; if (suppliedTitle != null) { pageTitle = suppliedTitle; } else { pageTitle = "Default Title"; }
The three forms result in the same value being stored into the variable named pageTitle
.
suppliedTitle
is referenced only once when using the ??
operator, and twice in the other two code examples.
The operator can also be used multiple times in the same expression:
return some_Value ?? some_Value2 ?? some_Value3;
Once a non-null value is assigned to number, or it reaches the final value (which may or may not be null), the expression is completed.
If, for example, a variable should be changed to another value if its value evaluates to null, since C# 8.0 the ??=
null coalescing assignment operator can be used:
some_Value ??= some_Value2;
Which is a more concise version of:
some_Value = some_Value ?? some_Value2;
In combination with the null-conditional operator ?.
or the null-conditional element access operator ?[]
the null coalescing operator can be used to provide a default value if an object or an object's member is null. For example, the following will return the default title if either the page
object is null or page
is not null but its Title
property is:
string pageTitle = page?.Title ?? "Default Title";
CFML
As of ColdFusion 11,[8] Railo 4.1,[9] CFML supports the null coalescing operator as a variation of the ternary operator, ?:
. It is functionally and syntactically equivalent to its C# counterpart, above. Example:
possiblyNullValue ?: valueIfNull
F#
The null value is not normally used in F# for values or variables.[10] However null values can appear for example when F# code is called from C#.
F# does not have a built-in null coalescing operator but one can be defined as required as a custom operator:[11]
let (|?) lhs rhs = (if lhs = null then rhs else lhs)
This custom operator can then be applied as per C#'s built-in null coalescing operator:
let pageTitle = suppliedTitle |? "Default Title"
Freemarker
Missing values in Apache FreeMarker will normally cause exceptions. However, both missing and null values can be handled, with an optional default value:[12]
${missingVariable!"defaultValue"}
or, to leave the output blank:
${missingVariable!}
Haskell
Types in Haskell can in general not be null. Representation of computations that may or may not return a meaningful result is represented by the generic Maybe type, defined in the standard library[13] as
data Maybe a = Nothing | Just a
The null coalescing operator replaces null pointers with a default value. The Haskell equivalent is a way of extracting a value from a Maybe by supplying a default value. This is the function fromMaybe.
fromMaybe :: a -> Maybe a -> a fromMaybe defaultValue x = case x of Nothing -> defaultValue Just value -> value
Some example usage follows.
fromMaybe 0 (Just 3) -- returns 3 fromMaybe "" Nothing -- returns ""
JavaScript
JavaScript's nearest operator is ??
, the "nullish coalescing operator," which was added to the standard in ECMAScript's 11th edition.[14] In earlier versions, it could be used via a Babel plugin, and in TypeScript. It evaluates its left-hand operand and, if the result value is not "nullish" (null
or undefined
), takes that value as its result; otherwise, it evaluates the right-hand operand and takes the resulting value as its result.
In the following example, a
will be assigned the value of b
if the value of b
is not null
or undefined
, otherwise it will be assigned 3.
const a = b ?? 3;
Before the nullish coalescing operator, programmers would use the logical OR operator (||
). But where ??
looks specifically for null
or undefined
, the ||
operator looks for any falsy value: null
, undefined
, ""
, 0
, NaN
, and of course, false
.
In the following example, a
will be assigned the value of b
if the value of b
is truthy, otherwise it will be assigned 3.
const a = b || 3;
Kotlin
Kotlin uses the ?:
operator.[15] This is an unusual choice of symbol, given that ?:
is typically used for the Elvis operator, not null coalescing, but it was inspired by Groovy (programming language) where null is considered false.
val title = suppliedTitle ?: "Default title"
Objective-C
In Obj-C, the nil coalescing operator is ?:
. It can be used to provide a default for nil references:
id value = valueThatMightBeNil ?: valueIfNil;
This is the same as writing
id value = valueThatMightBeNil ? valueThatMightBeNil : valueIfNil;
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 (in Perl terminology, 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. In the absence of side-effects 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.
Perl additionally has a //=
assignment operator, where
$a //= $b
is largely equivalent to:
$a = $a // $b
This operator differs from Perl's older ||
and ||=
operators in that it considers definedness, not truth. Thus they behave differently on values that are false but defined, such as 0 or '' (a zero-length string):
$a = 0; $b = 1; $c = $a // $b; # $c = 0 $c = $a || $b; # $c = 1
PHP
PHP 7.0 introduced[16] a null-coalescing operator with the ??
syntax. This checks strictly for NULL or a non-existent variable/array index/property. In this respect, it acts similarly to PHP's isset()
pseudo-function:
$name = $request->input['name'] ?? $request->query['name'] ?? 'default name'; /* Equivalent to */ if (isset($request->input['name'])) { $name = $request->input['name']; } elseif (isset($request->query['name'])) { $name = $request->query['name']; } else { $name = 'default name'; }
$user = $this->getUser() ?? $this->createGuestUser();/* Equivalent to */
$user = $this->getUser();
if (null === $user) {
$user = $this->createGuestUser();}
$pageTitle = $title ?? 'Default Title';/* Equivalent to */
$pageTitle = isset($title) ? $title : 'Default Title';
Version 7.4 of PHP will add the Null Coalescing Assignment Operator with the ??=
syntax:[17]
// The following lines are doing the same $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value'; // Instead of repeating variables with long names, the equal coalesce operator is used $this->request->data['comments']['user_id'] ??= 'value';
Python
Python does not have a null coalescing operator. Its functionality can be mimicked using a conditional expression:
now() if time is None else time
There was a proposal to add null-coalescing-type operators in Python 3.8, but that proposal has been deferred.[18]
Related functionality
Python's or
operator provides a related, but different behavior. The difference is that or
also returns the right hand term if the first term is defined, but has a value that evaluates to False
in a boolean context:
42 or "something" # returns 42 0 or "something" # returns "something" False or "something" # returns "something" "" or "something" # returns "something" [] or "something" # returns "something" dict() or "something" # returns "something" None or "something" # returns "something"
A true null coalescing operator would only return "something"
in the very last case, and would return the false-ish values (0
, False
, ""
, []
, dict()
) in the other examples.
PowerShell
Since PowerShell 7, the ??
null coalescing operator provides this functionality.[3]
$myVar = $null $x = $myVar ?? "something" # assigns "something"
Ruby
Ruby does not have a null-coalescing operator, but its ||
and ||=
operators work the same way except on Booleans. Ruby conditionals have only two false-like values: false
and nil
(where 'false' is not the same as 0). Ruby's ||
operator evaluates to its first operand when true-like. In comparison, Perl/Python/Javascript, which also have the latter property, have other false-like values (0 and empty string), which make ||
differ from a null-coalescing operator in many more cases (numbers and strings being two of the most frequently used data types). This is what led Perl/Python/Javascript to add a separate operator while Ruby hasn't.
Rust
While there's no null
in Rust, tagged unions are used for the same purpose. For example, Result<T, E>
or Option<T>
.
Any type implementing the Try trait can be unwrapped.
unwrap_or()
serves a similar purpose as the null coalescing operator in other languages. Alternatively, unwrap_or_else()
can be used to use the result of a function as a default value.
// Option // An Option can be either Some(value) or None Some(1).unwrap_or(0); // evaluates to 1 None.unwrap_or(0); // evaluates to 0 None.unwrap_or_else(get_default); // evaluates to the result of calling the function get_default // Result // A Result can be either Ok(value) or Err(error) Ok(1).unwrap_or(0); // evaluates to 1 Err("oh no").unwrap_or(1); // evaluates to 1
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 that follows the same prototype pattern:
ISNULL(possibly_null_value, 'value if null');
Attention should be taken to not confuse ISNULL with IS NULL – the latter serves to evaluate whether some contents are defined to be NULL or not.
The ANSI SQL-92 standard includes the COALESCE function implemented in Oracle,[19] SQL Server,[20] PostgreSQL,[21] SQLite[22] and MySQL.[23] The COALESCE function returns the first argument that is not null. If all terms are null, returns null.
COALESCE(possibly_null_value[, possibly_null_value, ...]);
The difference between ISNULL and COALESCE is that the type returned by ISNULL is the type of the leftmost value while COALESCE returns the type of the first non-null value.
Swift
In Swift, the nil coalescing operator is ??
. It is used to provide a default when unwrapping an optional type:
optionalValue ?? valueIfNil
For example, if one wishes to implement some Swift code to give a page a default title if none is present, one may use the following statement:
var suppliedTitle: String? = ... var pageTitle: String = suppliedTitle ?? "Default Title"
instead of the more verbose
var pageTitle: String = (suppliedTitle != nil) ? suppliedTitle! : "Default Title";
VB.NET
In VB.NET the If
[24] operator/keyword achieves the null coalescing operator effect.
Dim pageTitle = If(suppliedTitle, "Default Title")
which is a more concise way of using its variation
Dim pageTitle = If(suppliedTitle <> Nothing, suppliedTitle, "Default Title")
See also
- ?: (conditional)
- Elvis operator (binary ?:)
- Null-conditional operator
- Operator (programming)
References
- ↑ BillWagner. "?? Operator (C# Reference)". http://msdn.microsoft.com/en-us/library/ms173224.aspx.
- ↑ "ECMA-334, 3rd edition, June 2005". Ecma International. June 2005. p. 63. https://www.ecma-international.org/wp-content/uploads/ECMA-334_3rd_edition_june_2005.pdf.
- ↑ 3.0 3.1 "PowerShell 7 Preview 5" (in en-US). 2019-10-23. https://devblogs.microsoft.com/powershell/powershell-7-preview-5/.
- ↑ "perlop - perldoc.perl.org". http://perldoc.perl.org/perlop.html#C-style-Logical-Defined-Or.
- ↑ "The Swift Programming Language (Swift 4): Basic Operators". https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-XID_124.
- ↑ "PHP: News Archive - 2015". http://php.net/archive/2015.php#id2015-11-12-1.
- ↑ "Bash man page". https://linux.die.net/man/1/bash.
- ↑ "Elvis operator". https://wikidocs.adobe.com/wiki/display/coldfusionen/Elvis+operator.
- ↑ "[RAILO-2195 add support for the Elvis Operator - JBoss Issue Tracker"]. https://issues.jboss.org/browse/RAILO-2195.
- ↑ cartermp. "Null Values (F#)". http://msdn.microsoft.com/en-us/library/dd233197.aspx.
- ↑ cartermp. "Operator Overloading (F#)". http://msdn.microsoft.com/en-us/library/dd233204.aspx.
- ↑ "Expressions". http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing.
- ↑ "Hackage". http://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#t:Maybe.
- ↑ "ECMAScript 2020 Language Specification". Ecma International. June 2020. http://www.ecma-international.org/ecma-262/11.0/index.html.
- ↑ "Null safety". http://confluence.jetbrains.com/display/Kotlin/Null-safety..
- ↑ "PHP: rfc:isset_ternary". https://wiki.php.net/rfc/isset_ternary.
- ↑ Kocak, Midori. "PHP RFC: Null Coalescing Assignment Operator". https://wiki.php.net/rfc/null_coalesce_equal_operator.
- ↑ "PEP 505 -- None-aware operators". https://www.python.org/dev/peps/pep-0505/.
- ↑ "Database SQL Language Reference". http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions023.htm#SQLRF00617.
- ↑ "COALESCE (SQL Server Compact)". https://technet.microsoft.com/en-us/library/ms174075.aspx.
- ↑ "PostgreSQL: Documentation: 9.1: Conditional Expressions". 27 October 2016. http://www.postgresql.org/docs/9.1/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL.
- ↑ "SQLite Query Language: Core Functions". http://www.sqlite.org/lang_corefunc.html.
- ↑ "MySQL :: MySQL 5.5 Reference Manual :: 12.3.2 Comparison Functions and Operators". http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_coalesce.
- ↑ dotnet-bot. "If Operator (Visual Basic)". https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator.
![]() | Original source: https://en.wikipedia.org/wiki/Null coalescing operator.
Read more |