-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Provide spec for V8 proposal constraints-in-overrides #6738
Open
RexJaeschke
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
RexJaeschke:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,81 @@ | ||
## Override with constraints | ||
|
||
In C# 8.0, we added a feature to permit the specification of certain type parameter constraints in an `override` method declaration. This is a placeholder for its specification. | ||
|
||
|
||
Rex Jaeschke's contribution to this spec, using section numbering from the draft-v8 Ecma standard | ||
|
||
## 14 Classes|14.6 Methods|14.6.1 General | ||
|
||
Replace | ||
|
||
> A *method_declaration* for an explicit interface member implementation shall not have any *type_parameter_constraints_clause*s. A generic *method_declaration* for an explicit interface member implementation inherits any constraints from the constraints on the interface method. Similarly, a method declaration with the `override` modifier shall not have any *type_parameter_constraints_clause*s and the constraints of the method’s type parameters are inherited from the virtual method being overridden. | ||
|
||
with | ||
|
||
> In the absence of *type_parameter_constraints_clause*s, a generic *method_declaration* for an explicit interface member implementation inherits any constraints from the constraints on the interface method. Similarly, in the absence of *type_parameter_constraints_clause*s, a method declaration with the `override` modifier inherits any constraints from the virtual method being overridden. | ||
|
||
## 14 Classes|14.6 Methods|14.6.5 Override methods | ||
|
||
Replace | ||
|
||
> The override declaration does not specify any *type_parameter_constraints_clause*s. Instead, the constraints are inherited from the overridden base method. | ||
|
||
with | ||
|
||
> If *type_parameter_constraints_clauses* is present, each of its *type_parameter_constraints* shall be `class` or `struct`, and for the constraint `class` in the override must correspond to a type parameter in the base method that is known to be a non-nullable reference type. Any type parameter that has the `struct` constraint in the override shall correspond to a type parameter in the base method that is known to be a non-nullable value type. In the absence of *type_parameter_constraints_clauses*, the constraints are inherited from the overridden base method, and for the constraint `class` a parameter type `T?` is interpreted as `System.Nullable<T>`. | ||
|
||
And add the following example: | ||
|
||
> *Example*: The following demonstrates how the overriding rules work when type parameters are involved: | ||
> | ||
> ```csharp | ||
> #nullable enable | ||
> class A | ||
> { | ||
> public virtual void Foo<T>(T? value) where T : class { } | ||
> public virtual void Foo<T>(T? value) where T : struct { } | ||
> } | ||
> class B: A | ||
> { | ||
> public override void Foo<T>(T? value) where T : class { } | ||
> public override void Foo<T>(T? value) where T : struct { } | ||
> } | ||
> ``` | ||
> | ||
> Without the type parameters in the overriding methods, the compiler won’t know which base method is being overridden. *end example* | ||
|
||
## 17 Interfaces|17.6 Interface implementations|17.6.2 Explicit interface member implementations | ||
|
||
Replace | ||
|
||
> It is a compile-time error for an explicit interface method implementation to include *type_parameter_constraints_clause*s. The constraints for a generic explicit interface method implementation are inherited from the interface method. | ||
|
||
With | ||
|
||
> If *type_parameter_constraints_clauses* is present in an explicit interface method implementation, each of its *type_parameter_constraints* shall be `class` or `struct`, and for the constraint `class` in the implementation must correspond to a type parameter in the interface method that is known to be a non-nullable reference type. Any type parameter that has the `struct` constraint in the implementation must correspond to a type parameter in the interface method that is known to be a non-nullable value type. In the absence of *type_parameter_constraints_clauses*, the constraints for an explicit interface method implementation are inherited from the interface method, and for the constraint `class` a parameter type `T?` is interpreted as `System.Nullable<T>`. | ||
|
||
> *Example*: The following demonstrates how the overriding rules work when type parameters are involved: | ||
> | ||
> ```csharp | ||
> #nullable enable | ||
> interface I | ||
> { | ||
> void Foo<T>(T? value) where T : class; | ||
> void Foo<T>(T? value) where T : struct; | ||
> } | ||
> | ||
> class C : I | ||
> { | ||
> void I.Foo<T>(T? value) where T : class { } | ||
> void I.Foo<T>(T? value) where T : struct { } | ||
> } | ||
> ``` | ||
> | ||
> Without the type parameters in the implementing methods, the compiler won’t know which method is being implemented. *end example* | ||
|
||
## 17 Interfaces|17.6 Interface implementations|17.6.4 Implementation of generic methods | ||
|
||
Strike | ||
|
||
> *Note*: When a generic method explicitly implements an interface method no constraints are allowed on the implementing method ([§14.7.1](classes.md#1471-general), [§17.6.2](interfaces.md#1762-explicit-interface-member-implementations)). *end note* |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec files are shown on docs.microsoft.com, so we should leave these descriptions out.