Talk:Elvis operator

Latest comment: 1 year ago by Matma Rex in topic Merge into Short-circuit evaluation proposal

Merge with Null_coalescing_operator?

edit

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)Reply

A merge sounds reasonable to me. --K0zka (talk) 11:47, 11 July 2014 (UTC)Reply

  DoneDavid Eppstein (talk) 22:36, 4 November 2014 (UTC)Reply
I have reverted this merge. The "elvis operator" is a shorthand form of the ternary operator. It is distinct from the null coalesce operator. —ajf (talk) 21:50, 29 July 2015 (UTC)Reply
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   and  ?? —David Eppstein (talk) 21:59, 29 July 2015 (UTC)Reply
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. —ajf (talk) 22:05, 29 July 2015 (UTC)Reply
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)Reply

C# 6 now also support this operator

edit

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)Reply

Many problems with regards to C#

edit

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

s = s1.ToString();

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

s = s1?.ToString();

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)Reply

Are the C# and JavaScript examples incorrect?

edit

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)Reply

Neither is correct, I must have missed that edit. Going to revert it. —ajf (talk) 00:53, 7 November 2015 (UTC)Reply

@Ajfweb: 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)Reply
Maybe. I have this page on my watchlist to try and spot these kinds of things. The edits aren't that frequent, though. —ajf (talk) 00:58, 7 November 2015 (UTC)Reply

Kotlin does have an Elvis operator

edit

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 (talkcontribs)

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. —ajf (talk) 16:13, 29 November 2015 (UTC)Reply

Description vs. Syntax?

edit

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)Reply

ObjectiveC seems to have the operator, too

edit

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)Reply

Who coined the term "Elvis operator"?

edit

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

edit

The following discussion is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.


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)Reply

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)Reply

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)Reply

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)Reply

Closing proposal to merge. Klbrain (talk) 10:57, 10 July 2021 (UTC)Reply
The discussion above is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.

Merge into Short-circuit evaluation proposal

edit
The following discussion is closed. Please do not modify it. Subsequent comments should be made in a new section. A summary of the conclusions reached follows.
No consensus to merge. Matma Rex talk 21:47, 1 November 2022 (UTC)Reply

In some languages, the Elvis operator ?: is a notation for a short-circuit or with last-value semantics. I see no reason to have a separate article for it -- and I see value in integrating it into the comparison table in this article. --Macrakis (talk) 20:19, 17 November 2020 (UTC)Reply

Merge See reasoning above. --Macrakis (talk) 20:19, 17 November 2020 (UTC)Reply
Because the Elvis operator has this meaning in some but not all languages, I oppose the merge as these are distinct topics best covered separately. Klbrain (talk) 17:02, 27 December 2021 (UTC)Reply
So what? Different languages have different operator sets and different syntax for them. Would you also want to see separate articles on or else, &&, etc.? --Macrakis (talk) 17:18, 5 February 2022 (UTC)Reply
Support. There is no single reason to keep an artical per every operator which evaluates to a similar result. I'm also going to propose to merge Null coalescing operator into this artcle because it's basically the same binary operation. AXONOV (talk) 11:12, 6 February 2022 (UTC)Reply
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:06, 11 February 2022 (UTC)Reply
So you think that the particular form of syntactic sugar used is worth an article? Should we also have separate articles for the conditional in Pascal "if ... then ...", Lisp "(cond (...) ...)", Scheme "(if ... ...)", C "if (...) ...", and Python "if ...: // ..."?
For that matter, the present article has a whole section on the varying operators used to represent short-circuit evaluation. What's so special about the Elvis syntax? --Macrakis (talk) 17:30, 28 March 2022 (UTC)Reply
Oppose merge, since it's going to be less helpful for a reader if we try to shove all the information from the elvis operator article in here (and I do think that information should be kept in Wikipedia), but I think it's fine to include the Elvis operator in the comparison table where appropriate. Dingolover6969 (talk) 09:15, 23 August 2022 (UTC)Reply
The discussion above is closed. Please do not modify it. Subsequent comments should be made on the appropriate discussion page. No further edits should be made to this discussion.

AXONOV (talk) 11:36, 6 February 2022 (UTC)Reply

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)Reply

The Perl case is lacking in "Languages supporting the Elvis operator"

edit

Perl has 2 operators for this :

  • || the logical or

https://perldoc.perl.org/perlop#C-style-Logical-Or

  • // the defined 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)Reply