Talk:Elvis operator

Merge with Null_coalescing_operator?
This article is merely a subset of the Null_coalescing_operator article, which has been around for much longer. They link to each other but say the same things. I recommend redirecting Elvis_operator to Null_coalescing_operator#PHP and removing this page. It's just duplication. — Preceding unsigned comment added by 72.37.171.52 (talk) 20:39, 7 July 2014 (UTC)

A merge sounds reasonable to me. --K0zka (talk) 11:47, 11 July 2014 (UTC)
 * ✅ —David Eppstein (talk) 22:36, 4 November 2014 (UTC)
 * I have reverted this merge. The "elvis operator" is a shorthand form of the ternary operator. It is distinct from the null coalesce operator. &mdash;ajf (talk) 21:50, 29 July 2015 (UTC)
 * We have articles on subjects, not on their names; see WP:NOTDICT. Since this operator has the same meaning as the null coalesce operator (with two parameters) and is clearly not the same meaning as the ternary operator (which takes three parameters) we should combine it in the article with the same meaning, rather than pretending that its varying syntax makes it something else. Or do you think we should have separate articles for the two mathematical notations $$e^x$$ and $$\exp x$$?? —David Eppstein (talk) 21:59, 29 July 2015 (UTC)


 * It does NOT do the same thing as the null coalesce operator, and pretending it does produces the muddled mess that the null coalesce page became. Half the examples on the page don't coalesce null, they do a ternary operator-style falsiness check, which even the page itself acknowledges is "not a true null coalesce operator". The "Elvis operator" is the binary form of ?:. If we must merge it somewhere, make it part of the ?: article. &mdash;ajf (talk) 22:05, 29 July 2015 (UTC)


 * It is currently MORE fucked up when we have to explain how some forms of ?: is doing a reference thing and others are doing a boolean thing. --Artoria2e5 🌉 18:23, 12 August 2020 (UTC)

C# 6 now also support this operator
I think it's worth mentioning now that C# 6 also support this operator. — Preceding unsigned comment added by 194.90.15.225 (talk) 08:25, 9 August 2015 (UTC)

Many problems with regards to C#
in C# the ?? operator is the null-coalescing operator. The operator referred to as Elvis is the ?. or null-conditional operator. The functionality of it is to avoid NullReferenceExceptions. If you try to take

for instance and s1 is null it throws the exception. The Elvis null-conditional in C# is

and says if s1 is null return null, otherwise perform ToString on it and return that.

Please forgive mistakes, this is my first contribution in forever. 68.152.20.6 (talk) 18:43, 2 June 2017 (UTC)

Are the C# and JavaScript examples incorrect?
Both of these examples in this article appear to show the ternary if operator instead of the Elvis operator. Are these examples still correct? Jarble (talk) 00:52, 7 November 2015 (UTC)

Neither is correct, I must have missed that edit. Going to revert it. &mdash;ajf (talk) 00:53, 7 November 2015 (UTC)
 * Factual errors like this one often linger for days or weeks before being corrected. Should we set up a peer review system for this article to prevent these errors from being propagated? Jarble (talk) 00:56, 7 November 2015 (UTC)
 * Maybe. I have this page on my watchlist to try and spot these kinds of things. The edits aren't that frequent, though. &mdash;ajf (talk) 00:58, 7 November 2015 (UTC)

Kotlin does have an Elvis operator
I'm pretty disgusted with the reversal without any good reason by afjweb. I've pointed to the Kotlin Elvis operator:

https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator

which some person thinks is not an elvis operator, without indicating why. I've put in the example code of the Kotlin pages itself:

val l = b?.length ?: -1 Where b?.length may or may not evaluate to null.

@afjweb Would you be so kind to reverse the change? You may want to think a bit deeper and give a good reason before reversing these kind of edits, especially if you go straight against the author of a language itself (who the hell are you?). — Preceding unsigned comment added by Owlstead (talk • contribs)


 * My name here is Ajfweb, not afjweb. I've reverted it since while Kotlin has an operator with the ?: syntax, its behaviour is not that of a short ternary, but that of a null coalesce operator. Thus, it is not the "Elvis operator" in the sense that other languages use. &mdash;ajf (talk) 16:13, 29 November 2015 (UTC)

Description vs. Syntax?
The description states Elvis is used to "obtain a value that is specified in the second argument if the first argument is false, and to return the value of the first argument otherwise". So the first argument should be a bool, and the check is for the argument being true or false, not being null or not null. On the other hand, under syntax the article says Elvis is equivalent to: if (foo != null) var = foo; else var = bar; This is clearly NOT a boolean comparison of foo being true or false, but a check for foo being null or not null, aka the null coalescing operator.

Btw., who defined Elvis operator vs. null coalescing operator in the first place? The "new" usage (writing Elvis, meaning null coalescing), seems much more sane to me than the "classic" definition. The classic definition basically says: Return "true" or any arbitrary value if false. What's the point of always returning "true" in the first case? In practical use, Elvis has turned into a null coalescing operator in most languages, just because of automatic casting from ANY type to a boolean that can be used for comparison. — Preceding unsigned comment added by 80.171.185.94 (talk) 09:20, 16 February 2016 (UTC)

ObjectiveC seems to have the operator, too
After clicking through some links from here, I think ObjectiveC uses the elvis operator with the same meaning than the others here. https://en.wikipedia.org/wiki/Null_coalescing_operator#Objective-C Should we add it to the list of languages that support the operator?87.190.14.40 (talk) 16:12, 5 January 2018 (UTC)

Who coined the term "Elvis operator"?
We have a section that claims that the "Elvis operator" was so named because "?:" looks like an emoticon of Elvis Presley. This seems plausible. The source for the claim is Joyce Farrell's "Java Programming":


 * Java 7 introduces a collapsed version of the ternary operator that checks for `null` values assigned to objects. The new operator is called the Elvis operator because it uses a question mark and colon together (?:); if you view it sideways, it reminds you of Elvis Presley.

(Fragment found with Google Book Search.) This paragraph is factually wrong: Java 7 did not support the Elvis operator, nor does any version of Java (so far). So the name is definitely not in any of the Java specifications.

As far as I can tell, someone (but I don't know who) proposed adding the ?: operator to Java 7, but the proposal was not accepted. Possibly this person used the term "Elvis operator" in their proposal. Possibly this person was the one who coined the phrase. But none of this is certain.

Proposed merge of Null coalescing operator into Elvis operator
The Elvis page currently has to explain two variants of, one of which is the same as the null-coalescing one. It wouldn't make sense to call any one of them "non-elvis". Artoria2e5 🌉 18:27, 12 August 2020 (UTC)

Disagree with Merge: While the Elvis operator can and does get used as a null-coalescing operator, it gets used for other things as well. It would just confuse things to merge this in. And merging in which language calls it this or has this would be messy. peterl (talk) 23:27, 12 August 2020 (UTC)

If we don't merge, then we should remove things like SQL's `COALESCE` from the list of "Languages supporting the Elvis operator" section, which is clearly not an Elvis operator but a null-coalescing operator. I'm in favor of removing most of the current content of this article, limiting it to explain the origin and reason of the Elvis reference, while mentioning it's used as a null-coalescing operator in some languages (linking that article). Content that is not duplicated should be moved to that article. --Elvaka (talk) 21:51, 12 October 2020 (UTC)

Disagree with merge: Those are very different concepts that tend to be mixed up (including the current version of the Null coalescing operator article), so it's very important to keep these apart and explain the differences. --dingensfünf 15:38, 5 March 2021 (UTC)
 * Closing proposal to merge. Klbrain (talk) 10:57, 10 July 2021 (UTC)

Merge into Short-circuit evaluation proposal
AXO NOV (talk) ⚑ 11:36, 6 February 2022 (UTC)
 * No — Short-circuit evaluation covers a principle whereas the Elvis and Null coalescing operators are specific operators. All three deserve an article — GhostInTheMachine talk to me 10:04, 11 February 2022 (UTC)

The Perl case is lacking in "Languages supporting the Elvis operator"
Perl has 2 operators for this : https://perldoc.perl.org/perlop#C-style-Logical-Or
 * || the logical or

https://perldoc.perl.org/perlop#Logical-Defined-Or — Preceding unsigned comment added by 176.166.206.65 (talk) 00:12, 3 April 2022 (UTC)
 * // the defined or