Safe navigation operator

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

In object-oriented programming, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional 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 or an array index).

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 is currently supported in languages such as Apache Groovy,[1] Swift,[2] Ruby,[3] C#,[4] Kotlin,[5] CoffeeScript 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 (or other characters used for chaining).

While the safe navigation operator and null coalescing operator are both null-aware operators, they are not operationally equivalent.



Safe navigation operator:[6]

def name = 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 =[0].name;


Optional chaining operator:[7]

let name = article?.author?.name

Optional subscript operator:

let author = articles?[0].author


Ruby supports the &. safe navigation operator (also known as the lonely operator) since version 2.3.0:[8]

name = article&.author&.name


In C# 6.0 and above, basic null-conditional operators ?. and ?[]:[9]

String name = articles?[0]?.author?.name;


Null safe invocation operator:[10]

var name = article?.author?.name

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

class Foo {
    var _bar: String as Bar

var foo: Foo = null

// the below will evaluate to null and not return a NullPointerException
var bar = foo.Bar


Safe call operator:[11]

val name = article?.author?.name

Perl 6[edit]

Safe method call:[12]

my $name = $article.?author.?name;


The safe navigation operator is not currently supported in Python, but it is currently being proposed for inclusion with the following syntax:[13]

# Proposed syntax, not yet part of the language:
name = article?.author?.name


Existential operator:[14]

zip = lottery.drawWinner?().address?.zipcode


Crystal supports the try safe navigation method [15]

name = article.try &.author.try &.name


Rust supports the ? and try!{} operators for structures implementing the Try trait[16].

// The preferred method of quick returning Errors
fn write_to_file_question() -> Result<(), MyError> {
    let mut file = File::create("my_best_friends.txt")?;
    file.write_all(b"This is a list of my best friends.")?;

// The previous method of quick returning Errors
fn write_to_file_using_try() -> Result<(), MyError> {
    let mut file = r#try!(File::create("my_best_friends.txt"));
    r#try!(file.write_all(b"This is a list of my best friends."));

See also[edit]


  1. ^ "6.1. Safe navigation operator". Retrieved 2016-01-28.
  2. ^ "Optional Chaining". Retrieved 2016-01-28.
  3. ^ "Ruby 2.3.0 Released". Retrieved 2016-01-28.
  4. ^ "Null-conditional Operators (C# and Visual Basic)". Retrieved 2016-01-28.
  5. ^ "Null Safety". Retrieved 2016-01-28.
  6. ^ "6.1. Safe navigation operator". Retrieved 2016-01-28.
  7. ^ "Optional Chaining". Retrieved 2016-01-28.
  8. ^ "Ruby 2.3.0 Released". Retrieved 2016-01-28.
  9. ^ "Null-conditional Operators (C# and Visual Basic)". Retrieved 2016-01-28.
  10. ^ "The Gosu Programming Language". Retrieved 2018-12-18.
  11. ^ "Null Safety". Retrieved 2016-01-28.
  12. ^ "Perl 6 Operators". Retrieved 2016-06-28.
  13. ^ "PEP 505 -- None-aware operators". Retrieved 2018-08-27.
  14. ^ "The Existential Operatior". Retrieved 2017-06-15.
  15. ^ "Crystal API: Object#try".
  16. ^ "Trait std::ops::Try". Retrieved 2019-02-26.

External links[edit]

  • PEP 505, discussing the possibility of safe navigation operators for Python