dotnet / dotnet/csharplang

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

Open
#1,284 19 comments 24 reactions 1 assignee Claimed by @gafter View on GitHub
Implemented Needs ECMA Spec Proposal champion Smallish Feature
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

- [ ] Proposal added
- [x] Discussed in LDM
- [x] Decision in LDM
- [x] 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 https://github.com/dotnet/roslyn/pull/24173. See also https://github.com/dotnet/csharplang/issues/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: https://github.com/dotnet/roslyn/pull/25995

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.