279 questions
1
vote
1
answer
89
views
Why does the null coalescing operator not work here? [duplicate]
I am trying to better understand how the nullish coalescing operator (??) in JavaScript is supposed to work. Can someone please explain why this:
let a = b ?? 0;
returns this:
Uncaught ...
0
votes
0
answers
30
views
Why does assignment with the null coalescing operator not compile? [duplicate]
I expected this assignment to work. It does not.
public class Foo {}
public class Bar {}
internal class Program
{
static void Main(string[] args)
{
Foo? foo = null;
Bar? bar ...
0
votes
1
answer
62
views
How to explain null-coalescing expression precedence evaluation with some operators?
The following code works fine. What is the logic behind the addition + being evaluated after the null-coalescing ??? How's that possible? Where is the doc explaining that?
int? tNullable = 2;
...
1
vote
1
answer
75
views
null-conditional, null-coalescing, enumerable and params keyword
I didn't know what better title to chose for the question as I don't know what's the problem. Consider the following small C# code:
static void Main(string[] args)
{
F1(null);
}
public static void ...
0
votes
1
answer
93
views
Checking for null does not suppress null dereference warning in Visual Studio 2022
I have this simple method to get a value from a wrapped DataTable:
public string GetValue(int nodeID, string attributeName)
{
DataRow row = this.dataTable.Rows[nodeID];
var result = row.Field&...
-1
votes
2
answers
121
views
Problem using PHP null coalescing together with ternary operator to reset a Session [duplicate]
I am trying to reset the Session with the below code:
<?=
$_SESSION['pettySuccess']??'';
(($_SESSION['pettySuccess']??'')=='') ?'': unset($_SESSION['pettySuccess']);
?>
the idea behind my ...
1
vote
2
answers
662
views
SyntaxError: Unexpected token '.' for optional chaining and Nullish coalescing [duplicate]
The issue was encountered after installing the aws s3 sdk.
I followed the suggested solutions from all other similar problems in the following manner:
-updated node version (even though I had version ...
2
votes
1
answer
226
views
PHP null coalescing with ternary operator (again)
The following code understandably yields "dev" when $_SERVER['MODE'] is not set:
$_SERVER['MODE'] ?? null === 'production' ? 'prod' : 'dev'
That is because $_SERVER['MODE'] ?? null resolves ...
1
vote
0
answers
773
views
"SyntaxError: Unexpected token ?" executing Next.JS scripts
I'm learning Next.JS by following the freecodecamp tutorial, and running npm run dev gives the following error output:
showAll: args["--show-all"] ?? false,
^
...
2
votes
2
answers
291
views
Thread safe singleton property in one line
How can I write the following code in one line?
private static LockSDP instance;
private static readonly object _lock = new();
public static LockSDP GetInstance
{
...
-1
votes
1
answer
533
views
Postgres - COALESCE syntax on insert
I am trying to do an INSERT within PostgreSQL query but getting an error when I try to implement COALESCE operator..
What I try to accomplish is that if the id value in team_member if found, insert ...
2
votes
1
answer
821
views
The null-coalescing operator and throw
I've just noticed that when using throw with the null-coalescing operator, you cannot just use the keyword "throw" on its own, an exception must be included with it. The following code ...
0
votes
0
answers
21
views
Assign if not null - Otherwise leave original value [duplicate]
I'm using this line below, which works fine. My question is more for sport and curiosity than for practicality.
$x = $y ?? $x;
(How) could I get this even shorter in PHP? Having the $x in this line ...
2
votes
1
answer
323
views
Is there a shorter way to write this in C# 8?
if (value2 != null)
{
value1 = value2;
}
? and ?? operators don't seem to be useful here. I thought of using the ternary operator.
value1 = (value2 != null) ? value2 : value1;
...
0
votes
1
answer
67
views
Is there a way in PHP to use null coalesing/nullsafe operators to default to not set any value?
I only see use cases were null coalesing can be used to chain values, but not to omit any value.
Am I missing something essential or is there really no shorthand way to write this:
$model->name = &...
16
votes
3
answers
7k
views
Why am I getting Undefined property: stdClass:: with PHP8 when using the nullsafe operator before an undeclared property?
Php 8.0 introduced the nullsafe operator which can be used like so $foo?->bar?->baz;.
I have a code sample running on php 8.1 that throws the error Undefined property: stdClass::$first_name even ...
1
vote
2
answers
118
views
Possible NullReferenceException - but why?
Let's assume I have a method:
private ObservableCollectionExtended<Record> myCollection;
public void SetLoadingProperty(bool isLoading)
{
if (!myCollection?.Any() ?? false)
return;
...
1
vote
0
answers
993
views
Null coalescing operator in if-condition
I'm being confused as to how the null coalescing operator in PHP works and I'd like some explanation for the following if possible:
I have an associative array representing data posted to the server. ...
2
votes
0
answers
645
views
NPM nullish coalescing not working aka Unexpected token?
I have a nodejs API server and am using the nullish coalescing opperater.
That worked great for me so far, however since today I am getting the error message
newsletter: req.body.newsletter ?? ...
0
votes
1
answer
464
views
Error while using Nullish coalescing operator operator
This is the recipe page component
const RecipePages = (props) => {
return (
<div>
< h5 className="page-header" > Steps</h5 >
<div className="...
-1
votes
1
answer
728
views
Cast to one of two types in C#
How can I cast to one of two types in C#?
Here is what I am trying to do:
public class BaseCl {}
public class Foo : BaseCl {}
public class Bar : BaseCl {}
BaseCl instance;
... some code which puts a ...
-1
votes
3
answers
75
views
Can I use echo statement immediately after a null coalescing operator?
Is there a way to implement the logic in my snippet?
Auth::check() ?? echo "<h1>Log in please</h1>";
Parse error: syntax error, unexpected token "echo"
Auth::check() ...
0
votes
2
answers
891
views
Using c# NULL coalescing operator with NOT NULL Scenarios
I am populating a Class using LINQ and LinkUrl is a string property of my class.I need to set a value only if a property is not null , other wise no need to assign any values
Currently The conditional ...
1
vote
0
answers
55
views
Assign if not null operator [duplicate]
Is there a shortcut to the following statement?
if(b!=null) a=b;
The best I can come up with is
a=b??a;
I want to assign b to a only if b is not null, otherwise I want to leave a unchanged.
The ...
1
vote
2
answers
500
views
Assignment of property if not null, possible with null-coalecing?
I'm trying to do smth like
Im trying to assign property on an object if this object is not null.
but standard form of non-null invocation does not work for assignment like this
socket?.Blocking = ...