Shorthand for: "Bool = !Bool" Instead use: "Bool = !!" (inverts it) #5756
Replies: 7 comments 22 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Sorry, but I cannot see why There are already multiple ways of inverting a bool value in C#. I don't see any benefit to adding yet another, especially one that is so unintuitive. |
Beta Was this translation helpful? Give feedback.
-
It seems to me that C# should have a unary-operator for "flip the bool", as the usage of this is about as common as the usage of "++", replacing "int = int + 1", with unary operator. Can anyone else think of a more acceptable unary-operator notation? Other options that come to mind: |
Beta Was this translation helpful? Give feedback.
-
The idea of a syntax that doesn't include an expression in the LHS, but provides the operator to be applied on the same expression in the RHS is obscure for C#. But I can see what it aims to provide. Aside from What can be done though is this pattern of It won't look that great, Currently I have no other proposals for tackling this problem. Unfortunately, expression repetition is necessary if you want to perform these operations, or you can do the following hacks:
Bitwise operation hacks are not something most C# developers are familiar with, meaning there currently is no real practical solution for this minor inconvenience. Some brainstorming is definitely required. |
Beta Was this translation helpful? Give feedback.
-
public static void Invert(this ref bool b)
=> b = !b;
// ...
someBool.Invert(); |
Beta Was this translation helpful? Give feedback.
-
Apple's Swift language recently adopted the "bool.toggle()" method (works like a C# extension method). Is the C# language group able to designate implicit methods like this? I.e. that "bool.Invert()" or "bool.Toggle()" always exists (without needing an extension). If so, then same thing may apply to numerics for "numeric.Negate()". These methods would behave the same as an extension method. Someone on another forum suggested that we simply allow "++" to increment bool -- which technically makes 0 to 1, then wraps 1 back 0... and thus is a toggle.
But if we were going to adopt a unary operator, I currently still prefer:
|
Beta Was this translation helpful? Give feedback.
-
It is for those who routinely use boolean logic, you'll see this kind of (admittedly unattractive) code all the time in certain domains, often when modifying or extracting specific bit regions within datum. |
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.
-
As this site has opened my mind to looking at my "decade-old unchanged coding style", to figure out how to adapt my style to take on the progress of the C# language.
I went looking for a solution to something that is fairly annoying that should have better notation - which is for flipping a bool value.
I often write stuff that looks like this:
And realize that there exists a fairly cryptic/mind-bending shorthand for this now, which is:
But that isn't an intuitive notation.
Instead, I'd suggest the following notation:
This avoids writing the same expression twice (can be error prone) and also avoids the cryptic "^= true" notation.
Thoughts?
EDIT: Note this proposal was edited based on the initial feedback of @CyrusNajmabadi - so his first response here no longer makes sense to this now edited proposal.
Beta Was this translation helpful? Give feedback.
All reactions