Skip to content
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

Add support for nonnull constraints #830

Open
wants to merge 3 commits into
base: draft-v8
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ primary_constraint
| 'class'
| 'struct'
| 'unmanaged'
| 'notnull'
;

secondary_constraints
Expand All @@ -434,7 +435,7 @@ Each *type_parameter_constraints_clause* consists of the token `where`, followed

The list of constraints given in a `where` clause can include any of the following components, in this order: a single primary constraint, one or more secondary constraints, and the constructor constraint, `new()`.

A primary constraint can be a class type, the ***reference type constraint*** `class`, the ***value type constraint*** `struct`, or the ***unmanaged type constraint*** `unmanaged`.
A primary constraint can be a class type, the ***reference type constraint*** `class`, the ***value type constraint*** `struct`, or the ***nonullable type constraint*** `notnull`.

A secondary constraint can be a *type_parameter* or *interface_type*.

Expand Down Expand Up @@ -490,6 +491,48 @@ Any constraints shall be consistent among dependent type parameters. If type par

It is valid for `S` to have the value type constraint and `T` to have the reference type constraint. Effectively this limits `T` to the types `System.Object`, `System.ValueType`, `System.Enum`, and any interface type.

The token `notnull` is neither a keyword nor a contextual keyword. When it is encountered, it will either:

- Bind to a type named `notnull`
- Bind to no type, in which case, it is interpreted as the nonnullable type constraint.

The nonnullable type constraint specifies that a type argument used for the type parameter shall be a non-nullable value type or non-nullable reference type.

Generic declarations that include the `notnull` constraint may be used in a nullable oblivious context; however, that constraint is ignored, and a warning shall be generated.

> *Example*: Consider the following:
>
> <!-- Example: {template:"standalone-lib-without-using", name:"TypeParameterConstraints0", expectedWarnings:["CS8714","CS8714","CS8631"], ignoredWarnings:["CS0168"]} -->
> ```csharp
> #nullable enable
> public class C { }
> public class A<T> where T : notnull { }
> public class B1<T> where T : C { }
> public class B2<T> where T : C? { }
> class Test
> {
> static void M()
> {
> // nonnull constraint allows nonnullable struct type argument
> A<int> x1;
> // warning: nonnull constraint prohibits nullable struct type argument
> A<int?> x2;
> // nonnullconstraint allows nonnullable class type argument
> A<C> x3;
> // warning: nonnull constraint prohibits nullable class type argument
> A<C?> x4;
> // nonnullable base class requirement allows nonnullable class type argument
> B1<C> x5;
> // warning: nonnullable base class requirement prohibits nullable class type argument
> B1<C?> x6;
> // nullable base class requirement allows nonnullable class type argument
> B2<C> x7;
> // nullable base class requirement allows nullable class type argument
> B2<C?> x8;
> }
> }
> ```

If the `where` clause for a type parameter includes a constructor constraint (which has the form `new()`), it is possible to use the `new` operator to create instances of the type ([§12.8.16.2](expressions.md#128162-object-creation-expressions)). Any type argument used for a type parameter with a constructor constraint shall be a value type, a non-abstract class having a public parameterless constructor, or a type parameter having the value type constraint or constructor constraint.

It is a compile-time error for *type_parameter_constraints* having a *primary_constraint* of `struct` or `unmanaged` to also have a *constructor_constraint*.
Expand Down
Loading