Proposal: Definite assignment involving == and != when compared to a constant boolean value #801
Replies: 15 comments 4 replies
-
I disagree. When I encounter However, some folk, like you, prefer |
Beta Was this translation helpful? Give feedback.
-
The place I arrived to is that if (myCollection.Where(x => x != null).SelectMany(x => x.SomeProperty).Any() == false) So I aggree as well that the compiler should support both uses. |
Beta Was this translation helpful? Give feedback.
-
Also remember it's possible to be comparing to a const bool. |
Beta Was this translation helpful? Give feedback.
-
Just so you know of possibly related/migth-solve-this-use-case issues:
|
Beta Was this translation helpful? Give feedback.
-
I don't think this is a discussion of style. I would argue this is an actual compiler bug where |
Beta Was this translation helpful? Give feedback.
-
Absolutely right. Perhaps this should be moved to dotnet/roslyn. |
Beta Was this translation helpful? Give feedback.
-
This is the correct locatoin. The compiler is functioning as per the language spec. We'd need the language/spec to be updated in order to make any changes here in the compiler. |
Beta Was this translation helpful? Give feedback.
-
I agree that the current syntax is a little bit limiting but there is certainly a "can of worms" issue to contend with here and I think that the spec does need to draw a hard line in the sand somewhere. It's not a far leap to full-blown flow analysis, the expectation that the following would work: bool result = o is string s;
if (result) Console.WriteLine(s); And a myriad of other complex cases. Of course with flow analysis being potentially in the pipeline for C# non-nullable references maybe there could be some shared opportunities. And as this only expands on when a variable is definitely assigned it can be explored without possibly introducing breaking changes. |
Beta Was this translation helpful? Give feedback.
-
I would prefer to expand this slightly. The Boolean value for "definitely assigned when true" (DAWT) or "definitely assigned when false" (DAWF) may be evaluated as part of an expression, where the replacement of the Boolean value with either
|
Beta Was this translation helpful? Give feedback.
-
Is there anything simple programmer can do to push this issue further? |
Beta Was this translation helpful? Give feedback.
-
This is a request for a proposal, not a proposal, as it contains no details for how the language specification should be modified to accomplish the purpose. A first step to making this happen would be to get someone from the language design team interested enough to champion it. A next step for someone wanting this to happen would be to propose a set of changes to the language specification, and then prototype those changes in the compiler. These are all necessary steps. |
Beta Was this translation helpful? Give feedback.
-
We're nearly three years after the initial issue for this. Is there any movement here? |
Beta Was this translation helpful? Give feedback.
-
@skendrot As far as I know nobody has yet proposed a specific language change to accomplish this. If you feel this issue is important that would be a good first step for you to take. |
Beta Was this translation helpful? Give feedback.
-
Glad to know that this is finally fixed! |
Beta Was this translation helpful? Give feedback.
-
Definite assignment should take into account boolean expressions involving == and != when compared to a constant boolean value. This is continutation of thread from Roslyn.
This code is currently not valid and results in "Use of unassigned local variable" error.
However, logically identical code like this works:
We like to use
== flase
instead of!
in our code base for added readability. The!
is easily overlooked when skimming through the code.Beta Was this translation helpful? Give feedback.
All reactions