Type Parameter for Current Type in Generics #2493
Unanswered
phi1010
asked this question in
Language Ideas
Replies: 3 comments
-
@gafter Updated; finished writing. Can you remove that label again? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Implementing meta-functionality, such as custom IClonable-s, ICloneManager-s, IConvertableTo-s, ISynchronizable-s, etc. currently seems hard, without using runtime type checks. Some problems can be worked around by using excess type parameters and constraints, to other problems I don't see a solution only relying on type safety by compiler restraints.
I would like to define type constraints and parameters in interfaces with a new keyword such as
thistype
, which would represent an implicit type parameter that can optionally be explicitly defined, to add covariance/contravariance. It would be implicitly added to all derived classes/interfaces and represent the type actually instanciated; adding possibility to self-reference generic types (new MyClass<MyClass<MyClass<...>>>()
would take a while to write...).I think there was already a similar suggestion in the comments of a C#6 generics issue 1--2 years ago, but I wasn't able to find it anymore, it might have been closed with the ticket containing it.
ICloneable Example
new MyClass().Clone()
would have to return aMyClass
, as well as((BaseClass) new MyClass()).Clone()
would do. Such methods would currently either require being reimplemented in every class or being defined inheritable, if there are mechanisms allowing base class code to handle specialized objects:IParent-IChild Example
This already works in most cases, but requires a lot of code:
Using
thisclass
might make such type constraints much simpler:Beta Was this translation helpful? Give feedback.
All reactions