-
Notifications
You must be signed in to change notification settings - Fork 4k
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: null-coalescing assignment operator ??= #13737
Comments
Dupe of #205, #3366, #5163, #10249, #10556, #11394 and #12606. Per @gafter:
However, as mentioned at #205 (comment) there seems to be a decent amount of recurring interest in such an operator so there is a community prototype. |
@HaloFour makes me eat my words. |
This would probably make more sense along with #1276, so that x?.P = y;
if (x != null) x.P = y;
x?.P ??= y;
if (x != null) if (x.P == null) x.P = y; |
@alrz I find |
@orthoxerox they wouldn't be the same thing though the use cases are kinda similar. |
I understand that, but the effect of |
I'd be concerned about the subtleties of how that would be implemented. For example: // is the method called even if x is null and the result simply discarded?
x?.P = SomeComplexCalculation();
// are there any assignments if x or y are null?
x?.P = y?.P = z; Although I guess you could argue that assignment to a property isn't really any different from invoking a method: x?.P = y;
// same as:
x?.SetP(y);
// same as:
if (x != null) { x.SetP(y); } |
@HaloFour Beyond the simple case x ?= y ?= F(); // as a statement
var r = x ?= y ?= F(); // as an expression How these should be translated? I do prefer lazy evaluation of the right-hand-side, but it would be weird if PS: I like |
This promotes usage of mutability and |
You summarised my dislike of this proposal far more succinctly than I was going to. 😄 |
I disagree. You can't remove them either and some operations essentially need null and/or mutability, e.g. lazy initialization or caching. And since C# is not a pure functional language these patterns are pretty common (because you don't have the m-word to handle state). I'd argue that there should be some mechanism to manage mutability and null instead of discouraging them, namely, #5032 and #7626. |
It is actually possible to have your proverbial cake and eat it too, by utilizing Yes, it's a little more typing if you want to use it everywhere right now. Yes, there have been suggestions to add eg. a |
By "lazy initialization" I meant any initialization that occurs after construction (you don't always have access to the context from which the value is initialized), but sure, you could do it with a simple null check. More to the point, this operator is just a syntactical sugar which targets readability and conciseness. Anyways, I think the two proposals (#13737 and #1276) are complimentary and are more helpful together. |
I strongly dislike |
This is now being tracked at dotnet/csharplang#34 |
Proposal
I'd like to propose an idea for next C#, with name: null-coalescing assignment operator
We can combine the ?? and = operators (??=) like, += and -= ...
Sample:
myObject.MyStringList ??= new List<string>();
Assign if the left is null...
In C# 6 we had:
myObject.MyStringList = myObject.MyStringList ?? new List<string>();
Another sample:
(myObject.MyStringList ??= new List<string>()).Add("something");
This is very simple, but useful...
Thanks
The text was updated successfully, but these errors were encountered: