-
Notifications
You must be signed in to change notification settings - Fork 4.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
IDE work items for c# 9.0 patterns. #42368
Comments
cc @mikadumont for info |
Some other ideas for refactorings:
These might be useful to simplify current workarounds:
Some of these might overlap though, and we should decide which one should be an analyzer versus a refactoring.
I think this should be in the simplifier. There are other cases that need to be covered there.
This should include if-to-switch and switch-statement-to-expression in particular, those are specifically limited due to lack of or-patterns. Worth to consider:
|
great stuff. Thanks @alrz ! |
New diagnostics for pattern-matching in C# 9.0
|
Active statement tracking in switch expression arms works well. |
@sharwell I can take this if you're not working on it. A langversion-dependant reducer works here without a need for an analyzer, right? |
@alrz Sure go ahead! Also consider the following case, which is not new for C# 9 but would benefit from the same simplification:
|
Closing out. Any remaining work/enhancements can come through directed issues. |
Can you point me to more documentation on this limitation? I just stumbled upon it on a scenario where it didn't make a lot of sense to me: if (x is { Code: not 1 code })
{
return new Error(code);
} The value Can you elaborate why this is an invalid scenario? The restriction you mentioned doesn't seem to make sense when using constant values on the match IMHO. Seems like the only way out of this is degrading the code into using a if (x is { Code: var code } when code != 1)
{
return new Error(code);
} FAKE EDIT: Found a really weird (undocumented?) way of extracting the value, by repeating the same property twice: if (x is { Code: not 1, Code: var code })
{
return new Error(code);
} |
Because nothing in the language allows it. Neither hte 'not' nor 'constant' patterns allow you to name the result. We'd need a proposal allowing that for that to work :)
Now that is weird. @333fred @jcouv is that supposed to be allowed? Seems really really strange and unintentional. |
Huh. I agree it's odd, but I don't see anything in the spec that would disallow this: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/patterns.md#property-pattern. |
@julealgon Try: if (x is { Code: not 1 and var code })
{
return new Error(code);
} |
I agree @CyrusNajmabadi . I honestly thought it would give me some sort of error at runtime the first time I tried it, or match the values incorrectly.
It works @alrz ! Interesting... is that how it is supposed to be done at the end of the day? I don't think I've ever seen an example like that. |
Yes. This is definitely a supported and used pattern today. In the future, we might consider a way of making this more terse. But absent that, the above is def a realistic way to go. |
I wish this would be allowed object obj = 1;
if(obj is not int i or <= 0)
return;
// Do something with i when i is positive integer |
@Thaina I expect that would fall out of dotnet/csharplang#4018. |
Language/Compiler work:
dotnet/csharplang#2850dotnet/csharplang#3361These are the things we will need to support and/or validate:
is not null
. Useis not null
check when available. #43322and
,or
,not
patterns when appropriate. Supporting converting more switches to expressions if or-patterns are available #43324not
,and
,or
. Need to make sure we're at soft selecting in places where new comparison patterns are allowed so we don't interfere with typing. Add keyword completion for new C# 9 patterns #43365, Enable suggestion mode for certain cases in C# 9 pattern keyword completion #43574!(x is null)
tox is not null
. Updating our code-inverting tools to support inverting patterns. #43438(>=
Implement formatting for C# 9 patterns #43538not null
.case string _ =>
. Add analzyer that removes unnecessary _ .Add Using
,Generate type
.The text was updated successfully, but these errors were encountered: