Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: C# null conditional assignment #3366

Closed
HermanEldering opened this issue Jun 7, 2015 · 6 comments
Closed

Proposal: C# null conditional assignment #3366

HermanEldering opened this issue Jun 7, 2015 · 6 comments
Labels
Area-Language Design Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on.

Comments

@HermanEldering
Copy link

With the introduction of the null conditional operator it might make sense to also add null conditional assignment, for instance like this:

targetVariable ?= sourceVariable;

Which would be functional equivalent to:

// normal method
if (sourceVariable != null) targetVariable = sourceVariable; 
// slightly shorter method, but less clear (imo)
targetVariable = sourceVariable ?? targetVariable;
@gafter gafter added Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on. Area-Language Design labels Jun 8, 2015
@gafter
Copy link
Member

gafter commented Jun 8, 2015

I'm trying to understand why this would be a good thing.

@HermanEldering
Copy link
Author

It would be similar to the ?. operator in that it reduces the verbosity of the code, so it's advantages are similar. Admittedly, it is probably a less frequent operation. A disadvantage, compared with the null conditional operator, is that is less clear which variable is checked for null.

@whoisj
Copy link

whoisj commented Jun 8, 2015

I can see some value in an operator like this, though I'm not sure that it brings clarity of any kind.

Basically, the proposal is for a null-coalescing-assignment operator. It does reduce some verbosity in some cases, but there's a ton of holes in the current design like: how do you know which choice it made? It seems to a case for simply coalescing the following statement into a single statement:

object myRef = Init();

object testRef;
if ((testRef = Update()) != null)
{
    myRef = testRef;
}

// use myRef ...

into

object myRef = Init();
myRef ?= Update();
// use myRef ...

While I suppose I could see uses for this, I'm not it makes for overly clear code. Additionally, in the flow of things I don't have a way to know if the value of myRef is its initial value or the reallocated value and have surrendered flow control. If I explicitly do not care about its value, and only care that it has a value the null-coalescing operator is how to do default values.

object myRef = Update() ?? Init();
// use myRef ...

This is even more concise and is fairly explicit in that the value after the ?? is the default to value.

@paulomorgado
Copy link

Would it make a lot more sense to allow ?? to be composed with = (like +, -, etc.)?

targetVariable ??= sourceVariable;

@whoisj
Copy link

whoisj commented Jun 10, 2015

@paulomorgado interesting idea, but I don't think that fulfills the contract of the null-coalescing-assignment which is to say "assign a value but if it is null, use this default value instead".

The ?= / ??= operators are saying "assign a value if the it is not null, otherwise do not assign the value", which is still a like odd but maybe useful.

@paulomorgado
Copy link

That kind of is different than everything already in C#. Isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on.
Projects
None yet
Development

No branches or pull requests

4 participants