Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Safe navigation operator

From Wikipedia, the free encyclopedia
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 nestedifs, 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.

Examples

[edit]

Apex

[edit]

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#

[edit]

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

[edit]

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)

CoffeeScript

[edit]

Existential operator:[12]

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

Crystal

[edit]

Crystal supports thetry safe navigation method[13]

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

Dart

[edit]

Conditional member access operator:[14]

varname=article?.author?.name

Gosu

[edit]

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

Groovy

[edit]

Safe navigation operator and safe index operator:[1][16]

defname=article?.authors?[0].name

JavaScript

[edit]

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

Kotlin

[edit]

Safe call operator:[6]

valname=article?.author?.name

Objective-C

[edit]

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

[edit]

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;

PHP

[edit]

The null safe operator was accepted for PHP 8:[20]

$name=$article?->author?->name;

Raku (Perl 6)

[edit]

Safe method call:[21]

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

Ruby

[edit]

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

name=article&.author&.name

Rust

[edit]

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(())}

Scala

[edit]

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)

Swift

[edit]

Optional chaining operator,[3] subscript operator, and call:

letname=article?.authors?[0].nameletresult=protocolVar?.optionalRequirement?()

TypeScript

[edit]

Optional chaining operator was included in the Typescript 3.7 release:[4]

letx=foo?.bar?.[0]?.baz();

Visual Basic .NET

[edit]

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

See also

[edit]

References

[edit]
  1. ^ab"Support the optional path operator (?.)".GitHub. Retrieved2021-01-04.
  2. ^"Null-conditional Operators (C# and Visual Basic)". Retrieved2016-01-28.
  3. ^ab"Optional Chaining". Retrieved2016-01-28.
  4. ^ab"Typescript 3.7". Retrieved2019-11-06.
  5. ^ab"Ruby 2.3.0 Released". Retrieved2016-01-28.
  6. ^ab"Null Safety". Retrieved2016-01-28.
  7. ^ab"The question mark operator, ?". Retrieved2021-10-04.
  8. ^"MDN - optional chaining in JavaScript". 28 October 2024.
  9. ^"Salesforce Winter 21 Release Notes".Salesforce. Retrieved2020-10-13.
  10. ^"Member access operators (C# reference)".Microsoft Docs. Microsoft. Retrieved29 August 2019.
  11. ^"Threading Macros Guide". Retrieved2019-06-07.
  12. ^"The Existential Operatior". Retrieved2017-06-15.
  13. ^"Crystal API: Object#try".
  14. ^"Other Operators".A tour of the Dart language. Retrieved2020-01-08.
  15. ^"The Gosu Programming Language". Retrieved2018-12-18.
  16. ^"8.5. Safe index operator". Retrieved2020-09-25.
  17. ^"Optional Chaining in ECMAScript 2020".
  18. ^"Browser Support for Optional Chaining in JavaScript".
  19. ^"PPC 21 -- Optional Chaining".GitHub.
  20. ^"PHP: rfc:nullsafe_operator".wiki.php.net. Retrieved2020-10-01.
  21. ^"Raku Operators". Retrieved2022-09-16.
  22. ^A framework to create embedded Domain-Specific Languages in Scala: ThoughtWorksInc/Dsl.scala,ThoughtWorks Inc., 2019-06-03, retrieved2019-06-03
  23. ^"NullSafe: Kotlin / Groovy flavored null-safe ? operator now in Scala".Scala Users. 2018-09-12. Retrieved2019-06-03.
  24. ^"?. and ?() null-conditional operators (Visual Basic)".Microsoft Docs. Microsoft. Retrieved29 August 2019.

External links

[edit]
  • PEP 505, discussing the possibility of safe navigation operators for Python
Retrieved from "https://en.wikipedia.org/w/index.php?title=Safe_navigation_operator&oldid=1293253519"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp