In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional operator, null-propagation operator) is a binary operator that returns
null
Null may refer to:
Science, technology, and mathematics Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value
*Null character, the zero-valued ASCII character, also designated by , often used ...
if its first argument is null; otherwise it performs a dereferencing operation as specified by the second argument (typically an object member access, array index, or lambda invocation).
It is used to avoid sequential explicit null checks and assignments and replace them with method/property chaining. In programming languages where the navigation operator (e.g. ".") leads to an error if applied to a null object, the safe navigation operator stops the evaluation of a method/field chain and returns null as the value of the chain expression. It was first used by
Groovy 1.0 in 2007
and is currently supported in languages such as
C#,
Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIFT ...
,
TypeScript
TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. It is designed for the development of large appl ...
,
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
,
Kotlin,
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH), ...
and others. There is currently no common naming convention for this operator, but safe navigation operator is the most widely used term.
The main advantage of using this operator is that it avoids the
pyramid of doom
''Pyramid of Doom'' is a text adventure game written by Alvin Files and published by Adventure International in 1979. It is the eighth in the Scott Adams' ''Adventure'' series. Files independently reverse engineered Adams' ''Adventure'' engi ...
. Instead of writing multiple nested
if
s, programmers can just use usual chaining, but add question mark symbols before dots (or other characters used for chaining).
While the safe navigation operator and
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#, PowerShell as of version 7.0.0, Perl ...
are both null-aware operators, they are operationally different.
Examples
Apex
Safe navigation operator examples:
a .aMethod().aField // Evaluates to null if a null
a aMethod()?.aField // returns null if a aMethod() evaluates to null
String profileUrl = user.getProfileUrl()?.toExternalForm();
return ELECT Name FROM Account WHERE Id = :accId.Name;
C#
C# 6.0 and above have
?.
, the ''null-conditional member access operator'' (which is also called the ''Elvis operator'' by Microsoft and is not to be confused with the general usage of the term ''
Elvis operator
In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand. This is ident ...
'', whose equivalent in C# is
??
, the
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#, PowerShell as of version 7.0.0, Perl ...
) and
?[]
, the ''null-conditional element access operator'', which performs a null-safe call of an indexer accessor, get accessor. If the type of the result of the member access is a value type, the type of the result of a null-conditional access of that member is a
nullable version of that value type.
The following example retrieves the name of the author of the first article in an array of articles (provided that each article has an
Author
member and that each author has an
Name
member), and results in
null
if the array is
null
, if its first element is
null
, if the
Author
member of that article is
null
, or if the
Name
member of that author is
null
. Note that an
IndexOutOfRangeException
is still thrown if the array is non-null but empty (i.e. zero-length).
var name = articles? .Author?.Name;
Calling a lambda requires
callback?.Invoke()
, as there is no null-conditional invocation (
callback?()
is not allowed).
var result = callback?.Invoke(args);
Clojure
Clojure doesn't have true operators in the sense other languages uses it, but as it interoperable with Java, and has to perform object navigation when it does, the
some->
macro can be used to perform safe navigation.
(some-> article .author .name)
CoffeeScript
Existential operator:
zip = lottery.drawWinner?().address?.zipcode
Crystal
Crystal supports the
try
safe navigation method
name = article.try &.author.try &.name
Dart
Conditional member access operator:
var name = article?.author?.name
Gosu
Null safe invocation operator:
var name = article?.author?.name
The null-safe invocation operator is not needed for class attributes declared as Gosu Properties:
class Foo
var foo: Foo = null
// the below will evaluate to null and not return a NullPointerException
var bar = foo.Bar
Groovy
Safe navigation operator and safe index operator:
def name = article?.authors? name
JavaScript
Added in ECMAScript 2020, the optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be ''undefined'' or ''null''.
const name = article?.authors?. .name
const result = callback?.()
It short-circuits the whole chain of calls on its right-hand side: in the following example, ''bar'' is not "accessed".
null?.foo.bar
Kotlin
Safe call operator:
val name = article?.author?.name
Objective-C
Normal navigation syntax can be used in most cases without regarding NULLs, as the underlying messages, when sent to NULL, is discarded without any ill effects.
NSString *name = article.author name;
Perl 5
Perl 5 does not have this kind of operator, but a proposal for inclusion was accepted with the following syntax:
my $name = $article?->author?->name;
PHP
The null safe operator was accepted for PHP 8:
$name = $article?->author?->name;
Python
The safe navigation operator is not supported in Python. It was proposed for inclusion with the following syntax:
# Proposed syntax, not yet part of the language:
name = article?.author?.name
Raku (Perl 6)
Safe method call:
my $name = $article.?author.?name;
Ruby
Ruby supports the
&.
safe navigation operator (also known as the ''lonely operator'') since version 2.3.0:
name = article&.author&.name
Rust
Rust supports the
?
and
try!
operators for structures implementing the
Try
trait.
// The preferred method of quick returning Errors
fn write_to_file_question() -> Result<(), MyError>
// The previous method of quick returning Errors
fn write_to_file_using_try() -> Result<(), MyError>
Scala
The null-safe operator in Scala is provided by the library Dsl.scala.
val name = article.?.author.?.name : @ ?
The
@ ?
annotation can be used to denote a nullable value.
case class Tree(left: Tree @ ? = null, right: Tree @ ? = null, value: String @ ? = null)
val root: Tree @ ? = Tree(
left = Tree(
left = Tree(value = "left-left"),
right = Tree(value = "left-right")
),
right = Tree(value = "right")
)
The normal
.
in Scala is not null-safe, when performing a method on a
null
value.
a ullPointerExceptionshould be thrownBy
The exception can be avoided by using
?
operator on the nullable value instead:
root.?.right.?.left.?.value should be(null)
The entire expression is
null
if one of
?
is performed on a
null
value.
The boundary of a
null
safe operator
?
is the nearest enclosing expression whose type is annotated as
@ ?
.
("Hello " + ("world " + root.?.right.?.left.?.value)) should be("Hello world null")
("Hello " + (("world " + root.?.right.?.left.?.value.?): @ ?)) should be("Hello null")
(("Hello " + ("world " + root.?.right.?.left.?.value.?)): @ ?) should be(null)
Swift
Optional chaining operator,
subscript operator, and call:
let name = article?.authors? name
let result = protocolVar?.optionalRequirement?()
TypeScript
Optional chaining operator was included in the Typescript 3.7 release:
let x = foo?.bar?. .baz();
Visual Basic .NET
Visual Basic 14 and above have the
?.
(the ''null-conditional member access operator'') and
?()
(the ''null-conditional index operator''), similar to C#. They have the same behavior as the equivalent operators in C#.
The following statement behaves identically to the C# example above.
Dim name = articles?(0)?.Author?.Name
See also
*
Elvis operator
In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand. This is ident ...
*
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#, PowerShell as of version 7.0.0, Perl ...
References
{{Reflist
External links
PEP 505 discussing the possibility of safe navigation operators for Python
Operators (programming)
Conditional constructs
Articles with example code