This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Safe navigation operator" – news ·newspapers ·books ·scholar ·JSTOR(January 2016) (Learn how and when to remove this message) |
Inobject-oriented programming, thesafe navigation operator (also known asoptional chaining operator,safe call operator,null-conditional operator,null-propagation operator) is a binary operator that returnsnull 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 byGroovy 1.0 in 2007[1] and is currently supported in languages such asC#,[2]Swift,[3]TypeScript,[4]Ruby,[5]Kotlin,[6]Rust,[7]JavaScript,[8]and others. There is currently no common naming convention for this operator, butsafe navigation operator is the most widely used term.
The main advantage of using this operator is that it avoids thepyramid of doom. Instead of writing multiple nestedif
s, programmers can just use usual chaining, but add question mark symbols before dots (or other characters used for chaining).
While thesafe navigation operator andnull coalescing operator are bothnull-aware operators, they are operationally different.
Safe navigation operator examples inApex:[9]
a[x]?.aMethod().aField// Evaluates to null if a[x] == nulla[x].aMethod()?.aField// returns null if a[x].aMethod() evaluates to nullStringprofileUrl=user.getProfileUrl()?.toExternalForm();return[SELECTNameFROMAccountWHEREId=:accId]?.Name;
C# 6.0 and above have?.
, thenull-conditional member access operator (which is also called theElvis operator by Microsoft and is not to be confused with the general usage of the termElvis operator, whose equivalent in C# is??
, thenull coalescing operator) and?[]
, thenull-conditional element access operator, which performs a null-safe call of an indexerget accessor. If the type of the result of the member access is avalue type, the type of the result of a null-conditional access of that member is anullable version of that value type.[10]
The following example retrieves the name of the author of the first article in an array of articles (provided that each article has anAuthor
member and that each author has anName
member), and results innull
if the array isnull
, if its first element isnull
, if theAuthor
member of that article isnull
, or if theName
member of that author isnull
. Note that anIndexOutOfRangeException
is still thrown if the array is non-null but empty (i.e. zero-length).
varname=articles?[0]?.Author?.Name;
Calling a lambda requirescallback?.Invoke()
, as there is no null-conditional invocation (callback?()
is not allowed).
varresult=callback?.Invoke(args);
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, thesome->
[11] macro can be used to perform safe navigation.
(some->article.author.name)
Existential operator:[12]
zip=lottery.drawWinner?().address?.zipcode
Crystal supports thetry
safe navigation method[13]
name=article.try&.author.try&.name
Conditional member access operator:[14]
varname=article?.author?.name
Null safe invocation operator:[15]
varname=article?.author?.name
The null-safe invocation operator is not needed for class attributes declared as Gosu Properties:
classFoo{var_bar:StringasBar}varfoo:Foo=null// the below will evaluate to null and not return a NullPointerExceptionvarbar=foo.Bar
Safe navigation operator and safe index operator:[1][16]
defname=article?.authors?[0].name
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 beundefined ornull.[17] Major desktop browsers have supported this since 2020, and most mobile browsers added support by 2024.[18]
constname=article?.authors?.[0]?.nameconstresult=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
Safe call operator:[6]
valname=article?.author?.name
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[0].name;
Perl 5 does not have this kind of operator, but a proposal for inclusion was accepted with the following syntax:[19]
my$name=$article?->author?->name;
The null safe operator was accepted for PHP 8:[20]
$name=$article?->author?->name;
Safe method call:[21]
my$name =$article.?author.?name;
Ruby supports the&.
safe navigation operator (also known as thelonely operator) since version 2.3.0:[5]
name=article&.author&.name
Rust provides a?
operator[7] that can seem like a safe navigation operator. However, a key difference is that when?
encounters aNone
value, it doesn't evaluate toNone
. Instead, it behaves like areturn
statement, causing the enclosing function or closure to immediately returnNone
.
TheOption
methodsmap()
andand_then()
can be used for safe navigation, but this option is more verbose than a safe navigation operator:
fnprint_author(article:Option<Article>){println!("Author: {}",article.and_then(|y|y.author).map(|z|z.name).unwrap_or("Unknown".to_owned()));}
An implementation using?
will print nothing (not even "Author:") ifarticle
isNone
orarticle.unwrap().author
isNone
. As soon as?
sees aNone
, the function returns.
fntry_print_author(article:Option<Article>)->Option<()>{println!("Author: {}",article?.author?.name);Some(())}
The null-safe operator in Scala is provided by the library Dsl.scala.[22][23]
valname=article.?.author.?.name:@?
The@ ?
annotation can be used to denote a nullable value.
caseclassTree(left:Tree@?=null,right:Tree@?=null,value:String@?=null)valroot: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 anull
value.
a[NullPointerException]shouldbethrownBy{root.right.left.right.value// root.right.left is null!}
The exception can be avoided by using?
operator on the nullable value instead:
root.?.right.?.left.?.valueshouldbe(null)
The entire expression isnull
if one of?
is performed on anull
value.
The boundary of anull
safe operator?
is the nearest enclosing expression whose type is annotated as@ ?
.
("Hello "+("world "+root.?.right.?.left.?.value))shouldbe("Hello world null")("Hello "+(("world "+root.?.right.?.left.?.value.?):@?))shouldbe("Hello null")(("Hello "+("world "+root.?.right.?.left.?.value.?)):@?)shouldbe(null)
Optional chaining operator,[3] subscript operator, and call:
letname=article?.authors?[0].nameletresult=protocolVar?.optionalRequirement?()
Optional chaining operator was included in the Typescript 3.7 release:[4]
letx=foo?.bar?.[0]?.baz();
Visual Basic 14 and above have the?.
(thenull-conditional member access operator) and?()
(thenull-conditional index operator), similar to C#. They have the same behavior as the equivalent operators in C#.[24]
The following statement behaves identically to the C# example above.
Dimname=articles?(0)?.Author?.Name