SQL also has (or at least is supposed to have) a coalesce function; it's called coalesce. Perl's "or" can be used this way, and I suspect this may be true in other languages as well.
Vynce's suspicions are well-founded. The 'foo = bar || baz' method for getting the first non-falsy value works well in many other dynamically-typed languages, including Ruby and JavaScript. It can have unpredictable results in PHP, though, and is best avoided in that language.
Discussion (9)
Well, it wasn't supposed to explain it, it was more so that people who were aware of the operator but didn't know it's name could properly answer.
The function is simple though, it returns the first non-null value.
null ?? new Object()
returns the new object
new Object() ?? oldObject
returns the new object
oldObject ?? new Object()
returns oldObject if it's not null otherwise new object
I hope that made sence to people :)
If you're asking about the ?: operator, the conditional operator then:
the statement:
oldObject ?? new Object()
is equivalent to:
oldObject != null ? oldObject : new Object()
So the coalesce operator is a subset of the conditional operator.
My guess is that the or operator in C# returns either True or False. But I'm not sure...
Ah yes, ozamosi is correct. C# is strictly typed so the or-operator (valueA || valueB) only returns true or false.
SQL also has (or at least is supposed to have) a coalesce function; it's called coalesce. Perl's "or" can be used this way, and I suspect this may be true in other languages as well.
Vynce's suspicions are well-founded. The 'foo = bar || baz' method for getting the first non-falsy value works well in many other dynamically-typed languages, including Ruby and JavaScript. It can have unpredictable results in PHP, though, and is best avoided in that language.
well, PHP is best avoided in that language.
And Go is better than chess, too. We know, we know, already! good grief ...
"And Go is better than chess, too."
Victory for ZIM!
D'A
... 7k AGA