Jump to content

Elvis operator: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Simon04 (talk | contribs)
See also: +Option type
No edit summary
Line 43: Line 43:


==Analogous use of the short-circuiting OR operator==
==Analogous use of the short-circuiting OR operator==
In several languages, such as [[Common Lisp]], [[Lua (programming language)|Lua]], [[Perl]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], and [[JavaScript]], the [[operator (programming)|OR operator]] (typically <code>||</code> or <code>or</code>) 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 "shorted out of the circuit."
In several languages, such as [[C (programming language)|C]], [[C++]], [[Common Lisp]], [[Lua (programming language)|Lua]], [[Perl]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], and [[JavaScript]], the [[operator (programming)|OR operator]] (typically <code>||</code> or <code>or</code>) 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 "shorted out of the circuit."


==See also==
==See also==

Revision as of 09:23, 2 October 2018

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, ? :: the expression with the Elvis operator A ?: B is approximately equivalent to the expression with the ternary operator A ? A : B.

Some computer programming languages have different semantics for this operator. Instead of the first operand having to result in a boolean, it must result in an object reference. If the resulting object reference is not null, it is returned. Otherwise the value of the second operand (which may be null) is returned.

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

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"

Name

The name "Elvis operator" refers to its resemblance to an emoticon of Elvis Presley[1] (?:).

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.[2] This has been the case since at least GCC 2.95.3[3] (March 2001).
  • In Apache Groovy, the "Elvis operator" ?: is documented as a distinct operator;[4] this feature was added in Groovy 1.5[5] (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[6] (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's not null, and its right-hand side otherwise.[7] A common pattern is to use it with return, like this: val foo = bar() ?: return
  • In Gosu, the ?: operator also returns the right operand if the left is null.
  • In C#, the null-conditional operator, ?. is referred to as the "Elvis operator",[8] 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.
  • In the Xtend programming language, Elvis operator is present.[9]
  • In Google's Closure Templates, the Elvis operator is a null coalescing operator, equivalent to isNonnull($a) ? $a : $b.[10]
  • Swift supports this concept with its Nil-Coalescing Operator,[11] e.g. (a ?? b).
  • SQL supports this concept with its COALESCE function, e.g. COALESCE(a, b).

Analogous use of the short-circuiting OR operator

In several languages, such as C, C++, Common Lisp, Lua, 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 "shorted out of the circuit."

See also

References

  1. ^ Joyce Farrell. 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. ^ "Using the GNU Compiler Collection (GCC): Conditionals". gcc.gnu.org.
  3. ^ "Using and Porting the GNU Compiler Collection (GCC): C Extensions". gcc.gnu.org.
  4. ^ "Elvis Operator (?: )".
  5. ^ "The Apache Groovy programming language - Groovy 1.5 release notes". groovy-lang.org.
  6. ^ "PHP: Comparison Operators - Manual". PHP website. Retrieved 2014-02-17.
  7. ^ "Null Safety - Kotlin Programming Language". Kotlin.
  8. ^ Albahari, Joseph; Albahari, Ben (2015). C# 6.0 in a Nutshell (6 ed.). O'Reilly Media. p. 59. ISBN 978-1491927069.
  9. ^ Efftinge, Sven. "Xtend - Expressions". eclipse.org.
  10. ^ "Closure Template Concepts - Closure Templates". Google Developers.
  11. ^ "The Swift Programming Language (Swift 4.1): Basic Operators". developer.apple.com.