Jump to content

Talk:Null coalescing operator

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Why doesn't C++ have this?

[edit]

Honestly, it's ingenious. I for one tend to forget I'm trying to evaluate for null and would have to go back to the object identifying to type my '!'.ChazZeromus (talk) 23:43, 6 October 2009 (UTC)[reply]

several programming languages

[edit]

several programming languages support the null coalescing operator? Which are these? --Abdull (talk) 13:56, 8 November 2009 (UTC)[reply]

Python has a Null Coalescencing operator too -- Include?

[edit]

Python currently (2.6.2, likely even earlier) has a false-value coalescing operator "or". Consider the expression "a or b or c". It evaluates to "b or c" when "a" is false/empty, and thus to "c" when both "a" and "b" are false/empty. Unfortunately, this isn't a true null coalescence as the values "False", "0", "0.0", "[]", "()", "None", "\"\"", and "{}" all coalesce in the above construct even though they aren't actually all explicitly "False" even according to Python-- for instance, saying "[] == False" yields "False" while saying "0 == False" yields "True". Is it appropriate to add Python's take on a coalescing operator to this page? (Note: to settle confusion, boot up Python in your console and try the "x or y" and "x == y" items above.) --ButterSoda (talk) 07:00, 26 June 2010 (UTC)[reply]

Update on Python's null coalescing operator

[edit]

The above description is inaccurate -- We can consider Python's coalescing operator to be a true null coalescing operator. Below is a table comparing what each the languages Ruby, Python and Javascript will coalesce. I've taken it from my blog -- I understand that's not a valid resource, but now at least we can point toward what we need to verify with the official documents.

Expression as a left-operand  Coalesces in Ruby?  Coalesces in Python?  Coalesces in JavaScript?
nil / None / null             Yes                 Yes                   Yes
[]                            No                  Yes                   No
{}                            No                  Yes                   n/a*
0                             No                  Yes                   Yes
0.0                           No                  Yes                   Yes
""                            No                  Yes                   Yes
''                            No                  Yes                   Yes
false / False / false         Yes                 Yes                   Yes

--ButterSoda (talk) 15:18, 7 July 2010 (UTC)[reply]

You seem to have missed the point of a null coalescing operator. A null coalescing operator is typically used to provide a default value to use when a variable is null -- and only when it is null, not zero or any other value (including false). The only way an operator in your above table can be considered a null coalescing operator is if it has Yes for the top row and No for every other row. If your operator "?" returns 1 for "0 ? 1", then it is absolutely not a null coalescing operator, and if you use it as such you will shoot yourself in the foot. This is a common programming mistake, thinking "I'll just use an 'or' here to supply a default value", and then replacing actual meaningful zeros in the data with some nonzero value. Python's "or" behaves like Perl's "||", not like its "//". --dreish~talk 21:44, 15 February 2012 (UTC)[reply]

Dreish, is the article strictly about null coalescing operators? Or is it also inclusive of Logical Defined Or operators? If not, these others should be included in a separate articled for Logical Defined Or operators, and a link to it should be added to the "See Also" in this article. CURRENTLY, the article is inclusive of these extras, like Perl's "||", JavaScript's "||", PHP 5.3's "?:", and Python's logical defined or. If the article is not inclusive of these, please remove that from the introduction paragraph of the article and note it here in this talk page. Upon completion, I'll go ahead and create another article properly explaining Logical Defined Or operators and how they differ from Null Coalescing Operators. Additionally, examples from popular languages will be included, similar to this article. Swivelgames (talk) 00:21, 24 January 2013 (UTC)[reply]

"Logical defined or" is another term for a null coalescing operator; those aren't two different things. The mention of Perl's "||" is just to explain why it isn't a null coalescing operator, and why "//" was added to the language. If you want a section covering your personal favorite languages, go for it, but please don't misidentify a logical or operator as a logical defined or operator. --dreish~talk 13:38, 22 April 2013 (UTC)[reply]

Either wrong for Perl or for Javascript, Ruby and Python

[edit]

I'm not sure if Perl's defined-or operator does the same thing like C#'s null coalescing operator, but any attempts to indentify JS's, Ruby's and Python's operators with // are wrong.

The ||-operator works much the same in Perl and JS, whenever the Left-Hand-Side has a value which is FALSE in boolean context, the Right-H-S will be returned. For instance the number 0 or an empty string are false.

The //-operator in Perl returns the RHS IF AND ONLY IF the LHS is either "not declared" or "undefined" (e.g. any variable which wasn't initialized has the value undef)

look at this example code in the Perldebugger ("perl -de0"):

 DB<1>          print ( $i // "not declared" )
not declared
 DB<2> my $i;   print ( $i // "declared but undefined" )
declared but undefined
 DB<3> my $i=0; print ( $i // "defined" )
0
 DB<4> my $i=0; print ( $i || "defined but false" )
defined but false

Now try JS:

repl> var i=0
repl> i||"defined but false"
"defined but false"

Now Python:

>>> i=False
>>> i or "defined but false"
'defined but false'

And Ruby:

irb(main):025:0> i=false
=> false
irb(main):026:0> i||"defined but false"
=> "defined but false"


Cheers --188.97.68.187 (talk) 23:51, 8 August 2010 (UTC) LanX[reply]

History

[edit]

It feels very wrong to see an article on null-coalescing in which no reference is made to languages in which it originated (and from which it was copied in countless others, including bash and other shell scripting languages).

Please update the article with info from http://www.cs.arizona.edu/icon/refernce/prefix.htm#null and investigate whether SNOBOL also had those operators.

Kourzanov (talk) 21:30, 23 February 2012 (UTC)[reply]

Why was PHP and JavaScript removed?

[edit]

In the beginning paragraph of the article, it introduces the "null coalescing operator (or Logical Defined Or operator)". With the knowledge that these are indeed same (Null Coalescing Operators and Logical Defined Or Operators), why is it that my addition to the page was removed? PHP 5.3 has recently added a Null Coalescing/Logical Defined Or operator, and JavaScript has always had a Null Coalescing/Logical Defined Or operator. These function almost identical to all the other languages listed. Please explain why these were removed. Thanks guys. Swivelgames (talk) 22:56, 23 January 2013 (UTC)[reply]

Those are logical or operators, not null coalescing operators. If an operator $ returns 1 for the expression x $ 1 for any value of x other than null, it is not a null coalescing operator. In JavaScript, || treats several non-null values as false, such as 0, false, NaN, and the empty string. Likewise, PHP's ?: operator is just an or operator. The reason it was added is that PHP's version of || doesn't return its first true operand, but a simple boolean TRUE or FALSE regardless of what was passed to it. PHP's ?: treats 0, 0.0, "", "0", and empty arrays all as false. These can still be useful operators, with due caution, but they can't be called null coalescing operators. --dreish~talk 13:29, 22 April 2013 (UTC)[reply]
As was previously expressed, are these not Logical Defined Or operators instead? If so, the first paragraph needs to be updated to exclude Logical Defined Or's, rather than grouping them together, and then removing them when they're added to the article. This seems counter-intuitive. 98.173.148.247 (talk) 17:06, 5 June 2013 (UTC)[reply]
Once again, as the first sentence of the article makes clear, "null coalescing operator" and "logical defined or operator" are two ways of referring to the same thing. PHP, JavaScript, Python, and Perl versions before 5.10 do not have a null coalescing operator, but most languages can accomplish the same thing with relatively little code. A logical or operator is not the same thing as a defined or operator, and it is not appropriate to offer one as "close enough" to something that is named to distinguish it from that very thing, no doubt in order to support the quasi-religious devotion to one's personal favorite language. In C#, the term "null coalescing operator" is typically used to describe the operator, and in Perl, the term "defined-or operator" is typically used, but they behave identically. In Common Lisp and Scheme, the term "or operator" is used instead of, and means the same thing as, "null coalescing operator" because in those languages the concepts of false and null are exactly identical (not just similar, or one a subset of the other, or "treated as identical" or whatever other nonsense one might want to use). Clear enough? --dreish~talk 14:18, 10 June 2013 (UTC)[reply]
Neither Common Lisp nor Scheme has anything corresponding to null. The test "null" tests something for being the empty list, Python [], a value which is false in Common Lisp and true in Scheme. It has nothing to do with Python None, C NULL, or Java and C# null. --John Cowan (talk) 12:50, 13 October 2019 (UTC)[reply]
This has been resolved by adding in both, and explaining the subtle language-specific differences. However, there is now also an extra article specifically for PHP that should probably be merged back into this one.72.37.171.52 (talk) — Preceding undated comment added 20:45, 7 July 2014 (UTC)[reply]
Appologies for being late the party, but while I can see what the difference is for JavaScript, what is the difference for PHP? Is it that the operators test for true/false rather than null? Because it seems like the C version would do the same. Could someone clarify for me? (and in the article?) neonKow 17:24, 12 September 2014 (UTC) — Preceding unsigned comment added by Neonkow (talkcontribs)

PHP example

[edit]

The PHP example seems unneccessarily complicated and also a bit contrived. Can anyone come up with a better one? I tried

$drink_to_serve =  $over_21 ? "beer" :  $fav_soda  ?: "water"  # equivalent to:

$drink_to_serve = ($over_21 ? "beer" :  $fav_soda) ?: "water"

# add parentheses to make the order of operations explicit

$drink_to_serve = $over_21 ? "beer" : ($fav_soda ?: "water")

But it turns out to be a poor example because the logic ends up working out the same anyway. neonKow 17:19, 12 September 2014 (UTC)

Etymology for "Elvis operator"

[edit]

Several languages refer to this as the "Elvis operator". A colorful name to have chosen - anyone know where that comes from? Is it an inside joke relating to THE Elvis (Presley)? Or just some other person who happened to be named Elvis? Lurlock (talk) 20:07, 20 July 2015 (UTC)[reply]

Hi Lurlock. It's called that because the question mark and period together, i.e. ?. looks like two eyes (the dots at the bottom) with a wavy curl of hair going up (the curve of the question mark). The curled wave of hair is the prototypical Elvis Presley look. Wookian (talk) 21:18, 29 July 2015 (UTC)[reply]

Clojure

[edit]

Example with (or ...) isn't correct:

(or nil false 1)

will get 1 instead of false — Preceding unsigned comment added by SerCe (talkcontribs) 13:09, 10 December 2015 (UTC)[reply]

Incorrect claim in C# section

[edit]

The C# section claims that ?? is logically equivalent to != null, but this is not true in general.  != can be overridden, but ?? cannot (it's basically doing ReferenceEquals(lhs, null)). This should at least be mentioned, as it's quite a gotcha if you're in a context where == and != have been overriden (as in Unity, for example). 69.244.0.74 (talk) 16:28, 20 October 2016 (UTC)[reply]

There is a merge proposal to consider

[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.
To not merge given that these are distinct topics best covered separately. Klbrain (talk) 00:09, 24 August 2022 (UTC)[reply]

The same as above discussion about Elvis operator. I propose we merge Null coalescing operator and leave redirections in place as there are basically no differences sshort circuit evaluation. Objections? --AXONOV (talk) 11:21, 6 February 2022 (UTC)[reply]

Survey
Support as nom per reasons of WP:REDUNDANT. We don't need multiple articles covering the same topic. --AXONOV (talk) 11:21, 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]
No as per GhostInTheMachine's response, they deserve separate articles rather than a discombobulation of multiple topics in a single article. DevSpenpai::talk 16:35, 28 March 2022 (UTC)[reply]
Oppose as per my opposition to the other merge, but also because the NCO is specifically, like, a short-circuit OR for nulls. That's the basic difference. I think that difference is enough. After all, XOR is just OR, but, like, not both, and it gets its own article. Ternary operator is basically just an if statement that evaluates to something, and it gets its own article. Plenty of languages have both sc-|| and nco, and the distinction is very important if they have both boolean/falsy types and nulls. I believe it's useful to the reader if we're able to cover them both in-depth, cross-linking as appropriate. Dingolover6969 (talk) 09:22, 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:32, 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:05, 11 February 2022 (UTC)[reply]