Safe navigation operator

In object-oriented programming, 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 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, TypeScript, Ruby, Kotlin, Rust 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. Instead of writing multiple nested 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 are both null-aware operators, they are operationally different.

Apex
Safe navigation operator examples:

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, whose equivalent in C# is  , the null coalescing 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 (provided that each article has an  member and that each author has an   member), and results in   if the array is , if its first element is  , if the   member of that article is  , or if the   member of that author is. Note that an  is still thrown if the array is non-null but empty (i.e. zero-length).

Calling a lambda requires, as there is no null-conditional invocation (  is not allowed).

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 macro can be used to perform safe navigation.

CoffeeScript
Existential operator:

Crystal
Crystal supports the  safe navigation method

Dart
Conditional member access operator:

Gosu
Null safe invocation operator:

The null-safe invocation operator is not needed for class attributes declared as Gosu Properties:

Groovy
Safe navigation operator and safe index operator:

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.

It short-circuits the whole chain of calls on its right-hand side: in the following example, bar is not "accessed".

Kotlin
Safe call operator:

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.

Perl 5
Perl 5 does not have this kind of operator, but a proposal for inclusion was accepted with the following syntax:

PHP
The null safe operator was accepted for PHP 8:

Raku (Perl 6)
Safe method call:

Ruby
Ruby supports the  safe navigation operator (also known as the lonely operator) since version 2.3.0:

Rust
Rust provides a  operator that can seem like a safe navigation operator. However, a key difference is that when  encounters a   value, it doesn't evaluate to. Instead, it behaves like a  statement, causing the enclosing function or closure to immediately return.

The  methods   and   can be used for safe navigation, but this option is more verbose than a safe navigation operator:

An implementation using  will print nothing (not even "Author:") if   is   or   is. As soon as  sees a , the function returns.

Scala
The null-safe operator in Scala is provided by the library Dsl.scala.

The  annotation can be used to denote a nullable value.

The normal  in Scala is not null-safe, when performing a method on a   value.

The exception can be avoided by using  operator on the nullable value instead:

The entire expression is  if one of   is performed on a   value.

The boundary of a  safe operator   is the nearest enclosing expression whose type is annotated as.

Swift
Optional chaining operator, subscript operator, and call:

TypeScript
Optional chaining operator was included in the Typescript 3.7 release:

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.