-
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
Is-expressions w/ patterns: Why is var
allows nulls through?
#981
Comments
📝 Closely related to #792 |
I don't believe that issue actually answered the core question, though. The two syntaxes represent different patterns. You have the "type pattern" which matches only on non-null values of the specified type, and then you have the "variable pattern" which matches anything. You have a separate "null pattern" which matches only The current behavior doesn't make a lot of sense in the context of pattern matching as it exists in C# 7.0, but with recursive patterns it will make more sense. Then the Person person = GetPerson();
if (person is Student { Grade is 4.0, FirstName is var firstName }) {
Console.WriteLine($"{firstName} is a perfect student!");
} The pattern matching proposal is here: https://github.com/dotnet/csharplang/blob/master/proposals/patterns.md |
The thing is that I've proposed #306 to make it explicit when you want to null guard that variable,
|
In the next update to pattern-matching in C#, we will be adding support for a property pattern. For example, if you have a if (o is Point {X is 3, Y is var y}) ... (syntax not necessarily final; we might use If the input is already of the type you're testing, you can omit it and that type is implied. For example, if you have a variable if (p is {X is 3, Y is var y}) ... You can also provide an identifier if you want to refer to the matched entity later: if (GetPoint() is {X is 3, Y is var y} p) ... The semantics of a property pattern is that it first tests if the input is non-null, and then if it is non-null it also matches the named properties against the provided patterns. You can have any number of nested property/pattern pairs. In this example, if As a side-effect, you get a non-null pattern "for free", because "zero" satisfies "any number": if (GetPoint() is {} p) // if the returned point is not null... Given that there is pattern |
Neat. Any meeting notes or proposal updates coming soon? |
We have not had any LDM meetings on pattern-matching recently. |
Proposal updates would be nice, then. The existing proposal is quite outdated and doesn't even reflect the behavior or syntax of what was released in C# 7.0. For example it still refers to the narrow scoping rules and custom |
This doesn't break in the debugger:
But this does:
The behavior is different even though, when I hover the mouse pointer over
var
in Visual Studio, it saysclass System.String
like in the first snippet.I like writing
if ( SomeExpression is SomeType something )
instead of justif ( SomeExpression != null )
since then I don't have to repeat/reevaluateSomeExpression
inside theif
block. But sometimesSomeType
is a mess of nested generic types that I don't want to repeat writing either. This language behavior, however, precludes me from usingis var
. I'd appreciate an explanation as to why it was decided to make the feature behave this way.The text was updated successfully, but these errors were encountered: