Support extension on generic type. #4968
Merged
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.
Closes #3960.
This PR enables the following code:
Our existing type system has almost everything to support this, in that we already allow things like:
Supporting extending
T
directly isn't that much different from theMyType<T>
case. The one challenge we need to overcome is we shoudn't be considering the extension for every existing type in the linkage. Instead, we disallow general unconstraint extension such asextension<T> T {...}
and require allT
to be constraint with some interfaceIFoo
. Then we can register this extension under theIFoo
decl.When considering whether
extension<T:IFoo> T:IBar{}
can be applied to some typeX
, we go through all known base facets ofX
and see if there are any facet corrsponding toIFoo
. If so, we will try to joinX
withT
, and if it is successful, then we add the extension to the facet list ofX
. However, do so effectively makesX
conform toIBar
, so we need to repeat the process and add anyextesnion<T:IBar> T : IBaz
to the facet list...The next thing to consider is to make sure not to recurse indefeinitely when there is a "circle" of extensions such as:
Theoretically we can make this work, since it just means that every type that conforms to
IFoo
also conforms toIBar
and vice versa. But this is currently not implemented in this PR and we will issue a diagnostic if such circular extension is detected.