Elvis operator

{{short description|Binary operator in computer programming}}

{{About|the use of a ?: operator as a binary operator|use as a ternary operator|?:}}

In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that evaluates its first operand and returns it if its value is logically true (according to a language-dependent convention, in other words, a truthy value), and otherwise evaluates and returns its second operand. The second operand is only evaluated if it is to be returned (short-circuit evaluation). 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 expression 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.{{cite book|title=Java Programming|author=Joyce Farrell|date=7 February 2013|isbn=978-1285081953|page=276|publisher=Cengage Learning |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.}}

A similar operator is the null coalescing operator, where the boolean truth(iness) check is replaced with a check for non-null instead. This is usually written ??, and can be seen in languages like C#{{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}} or Dart.{{cite web |title=Conditional expressions |url=https://dart.dev/language/operators#conditional-expressions |website=Dart Language |publisher=Google}}

Alternative syntaxes

In several languages, such as Common Lisp, Clojure, Lua, Object Pascal, Python, Ruby, and JavaScript, there is no need for the Elvis operator, because the language's logical disjunction operator (typically || or or) is short-circuiting and returns its first operand if it would evaluate to a truthy value, and otherwise its second operand, which may be a truthy or falsy value (rather than a Boolean true or false value, such as in C and C++). These semantics are identical to the Elvis operator.

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 truthy, 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 f() twice if it yields truthy. Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result.

=Object reference variant=

{{main|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, which is universally regarded as falsy:

: x = f() ?: "default value"

Languages supporting the Elvis operator

  • In GNU C and C++ (that is: in C and C++ with GCC extensions), the second operand of the ternary operator is optional.{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc/extensions-to-the-c-language-family/conditionals-with-omitted-operands.html|title=Using the GNU Compiler Collection (GCC): Conditionals with omitted operands|website=gcc.gnu.org}} This has been the case since at least GCC 2.95.3 (March 2001), and seems to be the original Elvis operator.{{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}}
  • In Apache Groovy, the "Elvis operator" ?: is documented as a distinct operator;{{cite web|url=http://docs.groovy-lang.org/latest/html/documentation/index.html#_elvis_operator|title=Elvis Operator (?: )}} this feature was added in Groovy 1.5{{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}} (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.{{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}} (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.{{cite web|url=https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator|title=Null Safety - Kotlin Programming Language|website=Kotlin}} A common pattern is to use it with return, like this: {{code|2=kotlin|1=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",{{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}} 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.{{cite web|url=https://eclipse.org/xtend/documentation/203_xtend_expressions.html#elvis-operator|title=Xtend - Expressions|first=Sven|last=Efftinge|website=eclipse.org}}
  • In Google's Closure Templates, the Elvis operator is a null coalescing operator, equivalent to isNonnull($a) ? $a : $b.{{cite web|url=https://github.com/google/closure-templates/blob/master/documentation/reference/expressions.md#null-coalescing-operator--null-coalescing-operator|title=Closure Templates - Expressions|website=GitHub|date=29 October 2021}}
  • In Ballerina, the Elvis operator L ?: R returns the value of L if it's not nil. Otherwise, return the value of R.{{cite web|url=https://ballerina.io/learn/by-example/elvis-operator.html|title=Elvis Operator - Ballerina Programming Language|website=Ballerina|access-date=2018-12-19|archive-date=2018-12-20|archive-url=https://web.archive.org/web/20181220230754/https://ballerina.io/learn/by-example/elvis-operator.html|url-status=dead}}
  • 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.{{Cite web |title=Nullish coalescing operator (??) - JavaScript {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing |access-date=2023-01-05 |website=developer.mozilla.org |language=en-US}}

See also

References