Support Inheriting from Generic Type (CS0689) #79887
Replies: 4 comments 4 replies
-
This would certainly be quite a significant change, so I think to even start the discussion, you would need to:
|
Beta Was this translation helpful? Give feedback.
-
Keep in mind that C++'s templates are implemented in a fundamentally different way than .NET's generics. |
Beta Was this translation helpful? Give feedback.
-
C++ templates have monomorphization, which means generating code for specific generic application into concrete type, i.e. Foo<int> as a concrete type. As far as I know, C# generics does not yet support monomorphization, but it is definitely possible given how it is doing well with NativeAOT. |
Beta Was this translation helpful? Give feedback.
-
I would love this feature as a way to reduce boilerplate code when extending types from external assemblies. Let's assume thus example situation: // Base classes from an external assmebly
public class Base { }
public class A : Base
{
public Type Field;
public virtual Type Method() { }
}
public class B : Base
{
public Type OtherField;
public virtual Type OtherMethod() { }
} // My own project
public class Extended<T> : T where T : Base {
public Type NewField;
}
// Apart from inheriting
public class SuperA : Extended<A> { }
public class SuperB : Extended<B> { } While accessing/overriding members inherited specifically from |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Change .NET and C# to support inheriting from a generic type. (i.e. current compiler error CS0689)
This has been requested in forums before.
I believe it would be useful to extend common functionality to a variety of classes - even when that functionality requires backing data. And it would be better (and more exacting) solution than using a ConditionalWeakTable with extension methods.
Implementation challenges
I don't why defining types at runtime (or something...) would make this harder than using C++ templates to do the same thing. How is it substantially different than two dlls making a List<object> that ends up as a single type?
There would have to be some way to define 'make a base call' (either a dynamic call, or generating code when loading) rather than a static call - so that different base class implementations of a method could be called.
Monads
I only know a little about this concept, but I think that this style of inheritance would better support monads. If I want to extend the functionality of a class or interface, I can currently write extension methods. But those don't support state information. (Or make some hack with a ConditionalWeakTable) Unfortunately many of the interesting extensions that I've wanted to do require some state to be saved. This would be a natural way to extend a range of classes.
Beta Was this translation helpful? Give feedback.
All reactions