Apex Safe navigation operator examples in
Apex: a[x]?.aMethod().aField // Evaluates to null if a[x] == null a[x].aMethod()?.aField // returns null if a[x].aMethod() evaluates to null String profileUrl = user.getProfileUrl()?.toExternalForm(); return [SELECT 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, 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 Author member and that each author has a 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). string name = articles?[0]?.Author?.Name; Calling a lambda requires callback?.Invoke(), as there is no null-conditional invocation (callback?() is not allowed). Func callback = x => x * x; // squares x int result = callback?.Invoke(5);
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. An extended macro safe-> prevents calling nil values. (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 _bar: String as Bar } 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?[0].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. Major desktop browsers have supported this since 2020, and most mobile browsers added support by 2024. const name = article?.authors?.[0]?.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: my $name = $article?->author?->name;
PHP The null safe operator was accepted for
PHP 8: $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: that can seem like a safe navigation operator. However, a key difference is that when ? encounters a None value, it doesn't evaluate to None. Instead, it behaves like a return statement, causing the enclosing function or closure to immediately return None. The Option methods map() and and_then() can be used for safe navigation, but this option is more verbose than a safe navigation operator: fn print_author(article: Option) { 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:") if article is None or article.unwrap().author is None. As soon as ? sees a None, the function returns. fn try_print_author(article: Option) -> Option{ println!("Author: {}", article?.author?.name); Some(()) }
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[NullPointerException] should be thrownBy { 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.?.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, let x = foo?.bar?.[0]?.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==