Improved Definite Assignment Analysis #4240
Replies: 2 comments 2 replies
-
@jcouv, @cston, and I recently met to discuss whether to move forward with this essential approach or to try to do a more "thorough" approach involving introducing the concepts of "state when not null" and "state when null" which would propagate out of conditional accesses and be "converted" to "state when true" and "state when false" when comparisons are performed. We concluded that it is probably a prohibitive amount of work to go that way. For C# 10 we should go with the more piecemeal approach outlined in this specification. This way, we will address most of the scenarios that have been giving people pain, without spending an unreasonable amount of resources on the feature. Please feel free to follow up if you wish to amend this summary in any way 😉 |
Beta Was this translation helpful? Give feedback.
-
Improved Definite Assignment Analysis
Summary
Definite assignment analysis as specified has a few gaps which have caused users inconvenience. In particular, scenarios involving comparison to boolean constants, conditional-access, and null coalescing.
Related discussions and issues
Probably a dozen or so user reports can be found via this or similar queries (i.e. search for "definite assignment" instead of "CS0165", or search in csharplang).
https://github.com/dotnet/roslyn/issues?q=is%3Aclosed+is%3Aissue+label%3A%22Resolution-By+Design%22+cs0165
I have included related issues in the scenarios below to give a sense of the relative impact of each scenario.
Scenarios
As a point of reference, let's start with a well-known "happy case" that does work in definite assignment and in nullable.
Comparison to bool constant
Comparison between a conditional access and a constant value
This scenario is probably the biggest one. We do support this in nullable but not in definite assignment.
Conditional access coalesced to a bool constant
This scenario is very similar to the previous one. This is also supported in nullable but not in definite assignment.
Conditional expressions where one arm is a bool constant
It's worth pointing out that we already have special behavior for when the condition expression is constant (i.e.
true ? a : b
). We just unconditionally visit the arm indicated by the constant condition and ignore the other arm.Also note that we haven't handled this scenario in nullable.
Specification
The specification has moved to https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/improved-definite-assignment.md
Beta Was this translation helpful? Give feedback.
All reactions