-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Champion "Null-coalescing assignments" (16.3, Core 3) #34
Comments
Glad to see this feature graduated from the "we will never do this!" bucket. :) |
Yes please |
Very excited for this! Nice syntactic sugar similar to Ruby's |
@Thaina, what are the use cases for |
var mustAllTrue = Condition0;
mustAllTrue &&= Condition1;
mustAllTrue &&= Condition2; |
@Thaina You can already do that today with: var mustAllTrue = Condition0;
mustAllTrue &= Condition1;
mustAllTrue &= Condition2; |
@CyrusNajmabadi Wouldn't that still unconditionally evaluate the right-hand side? I'd expect in a situation such as:
that |
Ah, point taken. |
if Short circuiting would only happen for:
I'll agree that move statements to expressions can be more expressive. |
@paulomorgado true. 😅 |
@MadsTorgersen very glad to see you finally changed your mind on this "contrived" scenario ;) https://connect.microsoft.com/VisualStudio/Feedback/Details/585580 |
@Spongman To be fair, Mads said in that thread he thought |
dotnet/roslyn#13951 and dotnet/roslyn#1276 are also related here. obj?.Property = value;
obj?.Event += handler; |
Note that I did a prototype for this one a while ago, at ashmind/roslyn@cf5ee99. It was available on TryRoslyn for a while, but fell too far behind main roslyn branch at the moment -- I'll see if I can merge with latest. |
This proposal would allow to null check an expression without assigning it (if permitted), arg ??= throw new ArgumentNullException(nameof(arg));
// instead of
_ = arg ?? throw new ArgumentNullException(nameof(arg)); It could also be used with return, continue and break expressions. |
@Shawn-Twinmaple We could do this void DoStuff() => this.Data = this.Data ?? this.device?.ReadDataFromStream(); |
@Thaina The shorthand for that will be |
void DoStuff() => this.Data = this.device?.ReadDataFromStream() ?? this.Data; ? |
@Thaina Sort of, but not quite. In your example, you end up setting If you read Shawn's comment, he has the exact code you're looking for. |
@jnm2 I just think he just want to make it shorter to write. And so if it just a field I think it would have no side effect If we need to avoid side effect for property too, well, maybe we need another feature request I have seen about allow putting expression in operator void DoStuff() => this.Data = this.device?.ReadDataFromStream() ?? return; |
@Thaina Yes, that's exactly what he (and I) have just said. He wants it to be shorter to write. I really want that |
Yep exactly. That become this: Since both ! and != are already implemented negation operators I'm not really sure which "operator symbology" would work best for this. Symbology aside Ideally there would be a solution both for null and not-null assignments, as they are both very common place occurrences in code found everywhere. The proposed solution must work with nullable valuetypes and nullable reference types. I have attached some test examples that I believe covers all 4 of those cases for both proposed operators... I think the two operators really go hand-in-hand just like == and != does today; |
LDM Update: The result of the That means that #34 (comment) will happen (and hey, it looks like my exact example was used in the LDM 😄) |
@333fred and team, you're the best. Thanks for this! |
@gafter I think you hit the close+comment button by mistake. |
Does this feature mean I can finally use |
Yes. |
@jnm2 @yaakov-h dotnet/roslyn#36329 and #2591. We're hoping that this change will be in the final version of 16.2 or some preview before then. |
@333fred yay, thanks! |
Is it possible to make LHS of the null-coalescing assignments by-ref? using System;
class Program
{
static void Main()
{
string[] values = new string[1];
// OK
ref var r1 = ref values[0];
r1 ??= "abc";
Console.WriteLine(values[0]);
// NG
ref var r2 = ref values[0] ??= "default string";
}
} |
I love the idea of the ??= feature! I have been periodically checking updated compilers to see if it is included and was happy to see that it has a preview status. |
@ufcpp no compound assignment works that way, and it would conflict with the nullable-value-type change that was just implemented. |
@333fred This change made a real difference to me today. Thanks again! private bool? foo;
public bool Foo => foo ??= Properties.GetDataSourceRowByKeyValue(EditValue) is null; |
Introduces
??=
.Note: a minor change to
??
is included in this feature as implemented, so that it allows unconstrained type parameter types on the left.LDM history:
The text was updated successfully, but these errors were encountered: