null-coalescing-operator

is there a Java equivalent to null coalescing operator (??) in C#? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-26 10:30:07
问题 This question already has answers here : How to get the first non-null value in Java? (12 answers) Closed 2 years ago . Is it possible to do something similar to the following code in Java int y = x ?? -1; More about ?? 回答1: Sadly - no. The closest you can do is: int y = (x != null) ? x : -1; Of course, you can wrap this up in library methods if you feel the need to (it's unlikely to cut down on length much), but at the syntax level there isn't anything more succinct available. 回答2: Guava has

Is there a Python equivalent of the C# null-coalescing operator?

て烟熏妆下的殇ゞ 提交于 2019-11-26 07:24:41
问题 In C# there\'s a null-coalescing operator (written as ?? ) that allows for easy (short) null checking during assignment: string s = null; var other = s ?? \"some default value\"; Is there a python equivalent? I know that I can do: s = None other = s if s else \"some default value\" But is there an even shorter way (where I don\'t need to repeat s )? 回答1: other = s or "some default value" Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean

Coalesce function for PHP?

醉酒当歌 提交于 2019-11-26 06:05:17
问题 Many programming languages have a coalesce function (returns the first non-NULL value, example). PHP, sadly in 2009, does not. What would be a good way to implement one in PHP until PHP itself gets a coalesce function? 回答1: There is a new operator in php 5.3 which does this: ?: // A echo 'A' ?: 'B'; // B echo '' ?: 'B'; // B echo false ?: 'B'; // B echo null ?: 'B'; Source: http://www.php.net/ChangeLog-5.php#5.3.0 回答2: PHP 7 introduced a real coalesce operator: echo $_GET['doesNotExist'] ??

What is the “??” operator for? [duplicate]

不羁的心 提交于 2019-11-26 04:25:33
问题 This question already has answers here : What do two question marks together mean in C#? (17 answers) Closed 6 years ago . I was wondering about ?? signs in C# code. What is it for? And how can I use it? What about int? ? Is it a nullable int? See also: ?? Null Coalescing Operator —> What does coalescing mean? 回答1: It's called the "null coalescing operator" and works something like this: Instead of doing: int? number = null; int result = number == null ? 0 : number; You can now just do: int

PHP ternary operator vs null coalescing operator

拈花ヽ惹草 提交于 2019-11-26 01:46:28
问题 Can someone explain the differences between ternary operator shorthand ( ?: ) and null coalescing operator ( ?? ) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b 回答1: When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say: The null coalescing operator (??) has been added as syntactic sugar

What do two question marks together mean in C#?

倾然丶 夕夏残阳落幕 提交于 2019-11-25 23:04:08
问题 Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do the two question marks mean, is it some kind of ternary operator? It\'s hard to look up in Google. 回答1: It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN. FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); expands to: FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper(); which further expands to: if

Is there a “null coalescing” operator in JavaScript?

自闭症网瘾萝莉.ら 提交于 2019-11-25 21:56:40
问题 Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? \"Cookies!\"; The best approximation I can figure out for Javascript is using the conditional operator: var someString = null; var whatIWant = someString ? someString : \'Cookies!\'; Which is sorta icky IMHO. Can I do better? 回答1: The JavaScript equivalent of the C# null coalescing operator ( ?? ) is using a logical OR ( || ): var whatIWant = someString

PHP ternary operator vs null coalescing operator

爷,独闯天下 提交于 2019-11-25 18:44:40
Can someone explain the differences between ternary operator shorthand ( ?: ) and null coalescing operator ( ?? ) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b MasterOdin When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say: The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first