词条 | Elvis operator |
释义 |
In certain computer programming languages, the Elvis operator, often written Some computer programming languages (e.g. C#) have different semantics for this operator: instead of the first operand having to result in a boolean, it must result in an object reference.[1] If the resulting object reference is not null, it is returned. Otherwise the value of the second operand (which may also be null) is returned. This distinction is necessary because in C#, references are not implicitly convertible to a boolean.[2] The name "Elvis operator" refers to the resemblance of one of its notations, ExampleBoolean variantIn a language that supports the Elvis operator, something like this:
will set It is equivalent to this example, using the conditional ternary operator:
except that it does not evaluate the Object reference variantThis code will result in a reference to an object that is guaranteed to not be null. Function
Languages supporting the Elvis operator
Analogous use of the short-circuiting OR operatorIn several languages, such as Common Lisp, Lua, Perl, Python, Ruby, and JavaScript, the OR operator (typically See also
References1. ^{{cite web |title=?? Operator |url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator |website=C# Reference |publisher=Microsoft |accessdate=5 December 2018}} 2. ^{{cite web |title=The bool type |url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/types#the-bool-type |website=C# 6.0 draft specification |publisher=Microsoft |accessdate=5 December 2018}} 3. ^{{cite book|title=Java Programming|author=Joyce Farrell|isbn=978-1285081953|page=276|quote=The new operator is called Elvis operator because it uses a question mark and a colon together (?:); if you view it sideways, it reminds you of Elvis Presley.}} 4. ^{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals|title=Using the GNU Compiler Collection (GCC): Conditionals|website=gcc.gnu.org}} 5. ^{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC70|title=Using and Porting the GNU Compiler Collection (GCC): C Extensions|website=gcc.gnu.org}} 6. ^{{cite web|url=http://docs.groovy-lang.org/latest/html/documentation/index.html#_elvis_operator|title=Elvis Operator (?: )}} 7. ^{{cite web|url=http://groovy-lang.org/releasenotes/groovy-1.5.html|title=The Apache Groovy programming language - Groovy 1.5 release notes|website=groovy-lang.org}} 8. ^{{cite web |url=http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary |publisher=PHP website |title=PHP: Comparison Operators - Manual |accessdate=2014-02-17}} 9. ^{{cite web|url=https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator|title=Null Safety - Kotlin Programming Language|website=Kotlin}} 10. ^{{cite book |last1=Albahari |first1=Joseph |last2=Albahari |first2=Ben |date=2015 |title=C# 6.0 in a Nutshell |edition=6 |publisher=O'Reilly Media |isbn=978-1491927069 |page=59}} 11. ^{{cite web|url=https://eclipse.org/xtend/documentation/203_xtend_expressions.html#elvis-operator|title=Xtend - Expressions|first=Sven|last=Efftinge|website=eclipse.org}} 12. ^{{cite web|url=https://developers.google.com/closure/templates/docs/concepts#operators|title=Closure Template Concepts - Closure Templates|website=Google Developers}} 13. ^{{cite web|url=https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html|title=The Swift Programming Language (Swift 4.1): Basic Operators|website=developer.apple.com}} 14. ^{{cite web|url=https://ballerina.io/learn/by-example/elvis-operator.html|title=Elvis Operator - Ballerina Programming Language|website=Ballerina}} 4 : Operators (programming)|Binary operations|Conditional constructs|Articles with example code |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。