-
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
Proposal: Allow nullable types in declaration patterns #4724
Comments
Initial issue i see is the complexity of disambiguating this versus a ternary expression. |
Yeah, that's going to be too complicated. Though I think we could require parens around the pattern or the expression if we can't immediately disambiguate which should be rare. |
LDM looked at this on 5/19. There are some really gnarly parsing ambiguities here, but even if we could solve the compiler parsing problem, the human parsing problem will remain. We don't think those problems are really solveable, and that the gain isn't worth the complexity. |
Another important point that was raised in the discussion: part of the design for the nullability feature is that annotations do not affect code semantics. But here the |
Was that intended to permanently be the case? That would preclude combining them with new features or in new ways where they could affect the code semantics in useful ways. |
@HaloFour It's hard to tell until we've found an example where we want to break that rule. My understanding is likely yes (permanently) because we'll always have people that are using the latest version of C# without nullability enabled who later decide to annotate. So that concern will remain valid for new features even though nullability shipped a while back... |
Allow nullable types in declaration patterns
Summary
Allows the type to be specified in a var-pattern without change in semantics. This was originally proposed at #2039 but discarded due to possible behavioral change (see #2039 (comment) for an example).
Motivation
Currently, specifying the type in patterns has the side effect of checking for null which may not be the intent. Using nullable annotation would allow the type to be specified for readability without the added null check.
Detailed design
This would be only allowed if the type has an identity conversion to the input type, therefore, preserving the var-pattern semantics. The added restriction would make this mostly a code style preference and a safe replacement for var-pattern.
Note that the input type may not be immediately obvious, for example in a property pattern:
{ Prop: string? str }
.If the original input is already in a not-null state, a nullable type syntax will drop the null check which is currently emitted regardless of type annotations on the input type. However, in this case, the variable would still have the not-null state, as it is the case for a var-pattern in the same situation.
Unresolved questions
Design meetings
None.
The text was updated successfully, but these errors were encountered: