-
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: ??= #205
Comments
I agree the stepping through the debugger would be nice, but I don't think it would be clear whether
of
From your initial example it looks to be option 1, but the final use case would only work with option 2 (the semantics of |
@mirhagk I meant option 1 in both cases. |
I think it is a great idea. It might be worth to add a separate proposal for |
I've always wanted this operator as well. When assigning default values for a variable that has already been declared (or a parameter passed in) this could save a lot of boilerplate and condense two lines of code into one succinct statement.
vs.
|
@ashmind , I don't see a point in
|
@mburbea I'll create a separate item for those when I get a chance, to avoid confusing ??= discussion. |
I think this would be a great semantic improvement. It's obvious that the left is being assigned only if it's null. But the first exemple, at most, just needs a few parenthesis to work now. |
It seems unlikely we'd ever do this. |
@gafter Can you please elaborate? If it's the design, then it would be very useful good to discuss why, to avoid spending time on similar proposals (e.g. ||=). If it's the effort, then can't it be "Up For Grabs" instead of "Won't Fix"? |
The benefits of this proposal do not overcome the drawbacks in terms of language complexity. It is unlikely to ever have a high enough benefit-for-cost to make us prefer it over many other things we could do in this release or many releases into the future. |
Given the amount of interest in it (5 linked items) I've decided to try making a prototype -- which is currently at https://github.com/ashmind/roslyn/tree/features/coalesce-assignment. That's my first attempt at a Roslyn thing, so I'm sure I'm doing everything wrong. I've also added that branch to TryRoslyn: |
One open question from the prototype: I expect the answer to apply to any future implementations of |
@ashmind I think it should not perform the assignment if http://csharppad.com/gist/680db2bb9216f2909ddb226f10a91c18 Nice work! |
@ashmind I am pleased to see your prototype and delighted by its quality. The diff looks pretty clean and direct. The comments raise a couple good questions about the details of the correct behavior, particularly regarding type inference. I agree that when |
@alrz
@MgSam @judemelancon |
dotnet/csharplang#34 is this feature. |
??=
would have semantics analogous to the existing compound assignment operators like+=
,%=
, and<<=
. More precisely,x ??= y
would be equivalent tox = x ?? y
except thatx
is evaluated only once. It would have clear, if not vast, utility. Hopefully, it would not be an unreasonably large effort.It is perhaps debatable whether
looks better than
However, the latter seems easier to step through in the debugger.
Furthermore, a use case like
is less repetitive with
??=
.The text was updated successfully, but these errors were encountered: