T? is not allowed as a type pattern
See original GitHub issueVersion Used: master
Steps to Reproduce:
using System;
public class C {
void M(object g) {
_ = g is Nullable<int>; // ok
_ = g is int?; // ok
_ = g switch { Nullable<int> => 0 }; // CS8116
_ = g switch { int? => 0 }; // syntax error
switch (g) { case Nullable<int>: break; } // CS8116
switch (g) { case int?: break; } // syntax error
}
}
Expected Behavior: either
- No error in all cases
- CS8116 for
int?
type patterns as well.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Expression of type T cannot be handled by a pattern of type X
As explained by Neal Gafter from Microsoft: The reason it doesn't work is that there is no conversion (explicit or implicit) defined from...
Read more >Design pattern for "operation on object allowed, only if ...
Design pattern for "operation on object allowed, only if object is in certain state" For example: Only job applications that are not yet...
Read more >How to safely cast by using pattern matching and the is and ...
The pattern matching is statement doesn't allow a nullable value type, such as int? or Nullable<int> , but you can test for any...
Read more >What's up with type arguments in patterns?
Nieither seems to acknowledge that you can (kind of) have type parameters in patterns, just not in the way that you would expect....
Read more >Pattern Matching for instanceof
It is a compile-time error if a pattern variable is both introduced by a when false, and by b when false. As it...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I see two areas for improvement:
int?
when the same would be reported forNullable<int>
int?
toint
@Youssef1313
Oh, yes, I’d agree, it should be added as a warning wave if it is not currently. Note that SharpLab does not run in “strict” mode so you might need to check elsewhere.