Champion: "&&= and ||= assignment operators" #8872
Replies: 33 comments
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been hidden.
This comment has been hidden.
-
if( val != null &&= "some value"){ // do stuff } is this if statement a use case for the "assignment operators" feature? |
Beta Was this translation helpful? Give feedback.
-
Your sample looks like it's trying to do an equality check, not an assignment. It would not be part of assignment operators if it is not an assignment. |
Beta Was this translation helpful? Give feedback.
-
Here's an example of use - at least as far as I understand the proposal. Since the right-hand side won't be evaluated once public class CreateProfileView
{
private string _userName;
private string _password;
private string _passwordConfirmation;
// ...
private bool Validate()
{
bool result = string.IsNullOrWhiteSpace(_userName);
result &&= string.IsNullOrWhiteSpace(_password);
result &&= string.IsNullOrWhiteSpace(_passwordConfirmation);
result &&= string.Equals( _password, _passwordConfirmation, StringComparison.Ordinal);
result &&= MeetsRulesForUserNames(_userName);
result &&= MeetsComplexityRequirements(_password);
result &&= UserNameIsAvailable(_userName); // Checks our database for active users
result &&= PasswordHasNotBeenPwned(_password); // Calls out to the API at haveibeenpwned.com
return result;
}
} (Not that I'd actually write code like this - in the real world, we need actionable messages for the user, not just a simple true or false.) |
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek I don't think that's a very good example since you could just use |
Beta Was this translation helpful? Give feedback.
-
But on the other hand, if you're doing this in a loop, you should probably just exit early. Actually I'm not convinced this is a good feature at all 😄 |
Beta Was this translation helpful? Give feedback.
-
&&= would be useful for a loop that does constant-time equality checking to defeat timing attacks 😇 A very niche case, though. |
Beta Was this translation helpful? Give feedback.
-
An example of where I would have used this recently is with the visitor pattern: class FooVisitor
{
public bool BarFlag { get; private set; }
public void VisitNode(Node node)
{
BarFlag ||= ExpensiveCheck(node);
}
} |
Beta Was this translation helpful? Give feedback.
-
@Neme12 You're absolutely right - I was just trying to illustrate the point for @CodeDek and anyone else who was curious. Though, I've written code like this in cases when I needed to single step in a debugger.
Since I believe To me the example from @jnm2 is the most compelling one so far. |
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek oh yeah, duh. Whoops 🙈 |
Beta Was this translation helpful? Give feedback.
-
Sorry to bother, but could someone please help me understand what exactly this feature solves and why we need it? It seems like unnecessary complication to me, further steepening the learning curve of C# |
Beta Was this translation helpful? Give feedback.
-
Solves compound assignment of binary Boolean expressions. It doesn't complicate the language any more than any of the existing compound assignment operators, which happen to apply to almost every operator except |
Beta Was this translation helpful? Give feedback.
-
@HaloFour so correct me if I'm wrong, but will this:
Roughly expand to this?
Is that what it does? Is my understanding correct? |
Beta Was this translation helpful? Give feedback.
-
Minus the “var”. |
Beta Was this translation helpful? Give feedback.
-
TL;DR question: Is this proposal dead in the water and permanently rejected, if so why? This proposal was set to REJECTED on April 29, 2019, but I can find no mention of them in the LDM notes of any meeting of April 2019. The operators were discussed on July 16, 2018 as far as I can see, but the only thing mentioned in the notes was that they deserve their own proposal and should thus be splitted out from the ??= proposal. @MadsTorgersen could you tell me/us why this proposal was rejected? I started my own discussion (#6827) on this topic (because GitHub search had forsaken me). And was quickly pointed in this direction, and I can't gather from this issue (or the notes I looked through, maybe search is again failing me) why this proposal was rejected. The same was stated by @HaloFour in my short "discussion". And while @333fred has removed the REJECTED label, as far as I can gather, it was just done so this issue is more easily searchable.
Originally posted by @333fred in #397 (comment) So long story short... Why? |
Beta Was this translation helpful? Give feedback.
-
My recollection is that we rejected it because it just wasn't worth the effort. I was one pushing for it, and even I don't think it's that important. |
Beta Was this translation helpful? Give feedback.
-
I have no clue about the effort that goes into these things (just enormously grateful). And I do not wish to trivialize the effort, but to me (an relative outsider) the operator sounds like syntactic sugar over But isn't the fact that this issue was raised on at least 3 separate occasions here indicate that there is at least community support for these features? And why shouldn't it hold the same importance as other C# operators that we do have? I mean |
Beta Was this translation helpful? Give feedback.
-
Maybe worth reconsidering as a community contribution? |
Beta Was this translation helpful? Give feedback.
-
Definitely. But that still may not be important enough to us. We have literal finate resources. So time spent on this is time not spent on other things. I think we collectively just said it was fast too low to be worth it. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi like @HaloFour stated, is it perhaps possible to consider this by way of community contribution? Because @Rekkonnect already said he had made a prototype:"
|
Beta Was this translation helpful? Give feedback.
-
Possibly. I'll let @333fred weigh in. I know that community PRs are still very expensive for us. It helps with just one part of the costs here, but overall it's still something that will get likely push other work out. |
Beta Was this translation helpful? Give feedback.
-
Unless and until a member of the LDM decides to bring it for retriage, we won't accept pull requests for this feature. To speak to how much work this feature would require, my ballpark is several hundred hours of engineering time. It's not quite the simplest language feature (that would probably be around 100 engineering hours), but it's also not that bad. It needs:
The list above isn't fully complete, but it's got a lot of it. As you can see, while an external contributor can help with some of it, we're still looking at a significant investment of compiler team time, even for a small feature like this one. |
Beta Was this translation helpful? Give feedback.
-
Jup... my previous point stands:
|
Beta Was this translation helpful? Give feedback.
-
Then my follow-up question would be: how do we get somebody of the LDM to retriage this? By gathering 👍 emojis on this champion? |
Beta Was this translation helpful? Give feedback.
-
@synercoder Give them |
Beta Was this translation helpful? Give feedback.
-
By giving us persuasive examples of how this would improve code enough to be worth the effort. Remember, I said several hundred engineering hours of work above. From my own experience, I've been annoyed at the lack of a |
Beta Was this translation helpful? Give feedback.
-
I'm in the same boat. A couple of times a year do I want this. Unlike ??= Which I hit multiple times a week. |
Beta Was this translation helpful? Give feedback.
-
FWIW, I don't think I have ever needed a So yeah, I can see private bool _isWebCamTextureRunning;
private void Start()
{
_isWebCamTextureRunning = tryInitializeWebcam(_currentWebcamIndex);
}
private void Update()
{
// If a button is pressed...
StartCoroutine(sendPhoto());
}
private IEnumerator sendPhoto()
{
_isWebCamTextureRunning ||= tryInitializeWebcam(_currentWebcamIndex);
if (!_isWebCamTextureRunning)
yield return null;
// Send photo...
} |
Beta Was this translation helpful? Give feedback.
-
It's quite a handy thing to have in certain scenarios, including both |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions