Skip to content

Champion "permit t is null for unconstrained type parameter" (16.3, Core 3) #1284

Open

Description

  • Proposal added
  • Discussed in LDM
  • Decision in LDM
  • Finalized (done, rejected, inactive)
  • Spec'ed

Currently it is an error to write if (t is null) ... when the type of t is a type parameter that is not constrained to be a reference type. We should permit this to allow people to write that instead of if (t == null) ... uniformly when testing for null values. A code fixer to do just that has to have a special case for this and it feels quite irregular. See dotnet/roslyn#24173. See also #1261.

The current spec requires that the constant expression null be converted to the type of the value being tested. That would have to be changed (for type parameters) to make this work.

Related to that, we should also permit t is 3 when the type of t is a type parameter.

More generally, when the object being tested is an open type, we treat it the same as if it is of type object for the purposes of legality of the pattern.

This was implemented in dev16: dotnet/roslyn#25995

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions