Safe navigation operator


In object-oriented programming, the safe navigation operator is a binary operator that returns null if its first argument is null; otherwise it performs a dereferencing operation as specified by the second argument.
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 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 is currently supported in languages such as Apache Groovy, Swift, Ruby, C#, Kotlin, CoffeeScript, Scala, Dart 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 solves a problem commonly known as pyramid of doom. Instead of writing multiple nested ifs, programmers can just use usual chaining, but put question mark symbols before dots.
While the safe navigation operator and null coalescing operator are both null-aware operators, they are not operationally equivalent.

Examples

Groovy

Safe navigation operator:
def 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;

Swift

Optional chaining operator:
let name = article?.author?.name

Optional subscript operator:
let author = articles?.author

Ruby

Ruby supports the &. safe navigation operator since version 2.3.0:
name = article&.author&.name

C#

C# 6.0 and above have ?., the null-conditional member access operator and ?, the null-conditional element access operator, which performs a null-safe call of an indexer 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, 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.
var name = articles??.Author?.Name;

Visual Basic .NET

Visual Basic 14 and above have the ?. and ?, 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??.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

Kotlin

Safe call operator:
val name = article?.author?.name

Raku (Perl 6)

Safe method call:
my $name = $article.?author.?name;

Python

The safe navigation operator is not currently supported in Python, but it is currently being proposed for inclusion with the following syntax:
# Proposed syntax, not yet part of the language:
name = article?.author?.name

JavaScript

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?.author?.name

CoffeeScript

Existential operator:
zip = lottery.drawWinner?.address?.zipcode

TypeScript

Optional chaining operator was included in the Typescript 3.7 release:
let x = foo?.bar.baz;

Crystal

Crystal supports the try safe navigation method
name = article.try &.author.try &.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
val root: Tree @ ? = Tree,
right = Tree
),
right = Tree
)

The normal . in Scala is not null-safe, when performing a method on a null value.
a should be thrownBy
The exception can be avoided by using ? operator on the nullable value instead:
root.?.right.?.left.?.value should be
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 @ ?.
should be
) should be
should be

Clojure

Clojure doesn't have true operators in the sense other languages uses it, but as it interop with Java, and has to perform object navigation when it does, the some-> macro can be used to perform safe navigation.

Dart

Conditional member access operator:var name = article?.author?.name