Elvis operator

From HandWiki
Short description: Binary operator in computer programming

In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand. This is identical to a short-circuit or with "last value" semantics. The notation of the Elvis operator was inspired by the ternary conditional operator, ? :, since the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional A ? A : B.

The name "Elvis operator" refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an emoticon of Elvis Presley with his signature hairstyle.[1]

A similar operator is the null coalescing operator, where the boolean truth check is replaced with a check for non-null instead. This is usually written ??, and can be seen in languages like C#.[2]

Alternative syntaxes

In several languages, such as Common Lisp, Clojure, Lua, Object Pascal, Perl, Python, Ruby, and JavaScript, the OR operator (typically || or or) has the same behavior as the above: returning its first operand if it would evaluate to true in a boolean environment, and otherwise evaluating and returning its second operand. When the left-hand side is true, the right-hand side is not even evaluated; it is "short-circuited". This is different than the behavior in other languages such as C/C++, where the result of || will always be a boolean.

Example

Boolean variant

In a language that supports the Elvis operator, something like this:

x = f() ?: g()

will set x equal to the result of f() if that result is a true value, and to the result of g() otherwise.

It is equivalent to this example, using the conditional ternary operator:

x = f() ? f() : g()

except that it does not evaluate the f() twice if it is true.

Object reference variant

Main page: Null coalescing operator

This code will result in a reference to an object that is guaranteed to not be null. Function f() returns an object reference instead of a boolean, and may return null:

x = f() ?: "default value"

Languages supporting the Elvis operator

  • Perl since version v5.10 provides the Logical Defined Or operator: //, equivalent to defined $a ? $a : $b [3]
  • In GNU C and C++ (that is: in C and C++ with GCC extensions), the second operand of the ternary operator is optional.[4] This has been the case since at least GCC 2.95.3 (March 2001), and seems to be the original Elvis operator.[5]
  • In Apache Groovy, the "Elvis operator" ?: is documented as a distinct operator;[6] this feature was added in Groovy 1.5[7] (December 2007). Groovy, unlike GNU C and PHP, does not simply allow the second operand of ternary ?: to be omitted; rather, binary ?: must be written as a single operator, with no whitespace in between.
  • In PHP, it is possible to leave out the middle part of the ternary operator since PHP 5.3.[8] (June 2009).
  • The Fantom programming language has the ?: binary operator that compares its first operand with null.
  • In Kotlin, the Elvis operator returns its left-hand side if it is not null, and its right-hand side otherwise.[9] A common pattern is to use it with return, like this: val foo = bar() ?: return
  • In Gosu, the ?: operator returns the right operand if the left is null as well.
  • In C#, the null-conditional operator, ?. is referred to as the "Elvis operator",[10] but it does not perform the same function. Instead, the null-coalescing operator ?? does.
  • In ColdFusion and CFML, the Elvis operator was introduced using the ?: syntax.
  • The Xtend programming language has an Elvis operator.[11]
  • In Google's Closure Templates, the Elvis operator is a null coalescing operator, equivalent to isNonnull($a) ? $a : $b.[12]
  • In Ballerina, the Elvis operator L ?: R returns the value of L if it's not nil. Otherwise, return the value of R.[13]
  • In JavaScript, the nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.[14]

See also

References

  1. Joyce Farrell (7 February 2013). Java Programming. p. 276. ISBN 978-1285081953. "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." 
  2. "?? Operator". Microsoft. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator. Retrieved 5 December 2018. 
  3. "perlop - Perl operators and precedence -Perldoc Browser". Perl.org. https://perldoc.perl.org/5.10.0/perlop#C-style-Logical-Defined-Or. Retrieved 2023-01-19. 
  4. "Using the GNU Compiler Collection (GCC): Conditionals with omitted operands". https://gcc.gnu.org/onlinedocs/gcc/extensions-to-the-c-language-family/conditionals-with-omitted-operands.html. 
  5. "Using and Porting the GNU Compiler Collection (GCC): C Extensions". https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC70. 
  6. "Elvis Operator (?: )". http://docs.groovy-lang.org/latest/html/documentation/index.html#_elvis_operator. 
  7. "The Apache Groovy programming language - Groovy 1.5 release notes". http://groovy-lang.org/releasenotes/groovy-1.5.html. 
  8. "PHP: Comparison Operators - Manual". PHP website. http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary. Retrieved 2014-02-17. 
  9. "Null Safety - Kotlin Programming Language". https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator. 
  10. Albahari, Joseph; Albahari, Ben (2015). C# 6.0 in a Nutshell (6 ed.). O'Reilly Media. p. 59. ISBN 978-1491927069. 
  11. Efftinge, Sven. "Xtend - Expressions". https://eclipse.org/xtend/documentation/203_xtend_expressions.html#elvis-operator. 
  12. "Closure Templates - Expressions". 29 October 2021. https://github.com/google/closure-templates/blob/master/documentation/reference/expressions.md#null-coalescing-operator--null-coalescing-operator. 
  13. "Elvis Operator - Ballerina Programming Language". https://ballerina.io/learn/by-example/elvis-operator.html. 
  14. "Nullish coalescing operator (??) - JavaScript | MDN" (in en-US). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing.