Elvis operator
In certain computer programming languages, the Elvis operator ?:
is a binary operator that returns its first operand if that operand is true, and otherwise evaluates and returns its second operand. It is a variant of the ternary conditional operator, ? :
, found in those languages (and many others): the Elvis operator is the ternary operator with its second operand omitted.
Example
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:
x = f() ? f() : g()
except that it does not evaluate the f()
twice if it is true.
Name
The name "Elvis operator" refers to its resemblance to an emoticon of Elvis Presley.[1][2]
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.[3] This has been the case since at least GCC 2.95.3[4] (March 2001).
- In Apache Groovy, the "Elvis operator"
?:
is documented as a distinct operator;[5] this feature was added in Groovy 1.5[6] (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, the second operand of the ternary operator has been optional since PHP 5.3[7] (June 2009).
- The Fantom programming language has the
?:
binary operator that compares its first operand withnull
. - In Kotlin elvis operator returns expression that comes before it, if it's not null and expression after it otherwise.[8] Common pattern is to use it with return keyword
val foo = bar() ?: return
- In Gosu the ?: operator also returns the right operand if the left is null.
- In C#, the null coalescing operator,
??
has sometimes[9][10] been referred to as the "Elvis operator", but it does not perform the same function.
Analogous use of the OR operator
In several languages, such as Perl, Python, 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.
Comparison to the null coalescing operator
While Elvis operator compares to boolean false, Null coalescing operator compares to null.
See also
?:
or conditional operator, when used as a ternary operator- Null coalescing operator,
??
or//
operator - Safe navigation operator, often
?.
- Spaceship operator
<=>
References
- ↑ "coding style - ?: operator". Stack Overflow. Retrieved 2014-02-17. Lay summary – elvis emoticon graphic.
- ↑ Joyce Farrell. Java Programming. p. 276. ISBN 978-1285081953.
The new operator is called Elvis operator because it uses a question mark and a colo together (?:)
- ↑ https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals
- ↑ https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC70
- ↑ "Elvis Operator (?: )".
- ↑ http://groovy-lang.org/releasenotes/groovy-1.5.html
- ↑ "PHP: Comparison Operators - Manual". PHP website. Retrieved 2014-02-17.
- ↑ https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator
- ↑ http://stackoverflow.com/questions/27493541/null-conditional-operator-and-string-interpolation-in-c-sharp-6
- ↑ https://www.linqpad.net/download.aspx