question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

T? is not allowed as a type pattern

See original GitHub issue

Version 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:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sharwellcommented, Sep 28, 2020

I see two areas for improvement:

  1. Report CS8116 for use of int? when the same would be reported for Nullable<int>
  2. Add a code fix for this error to change int? to int
1reaction
HaloFourcommented, Sep 28, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found