- Proposed
- Prototype: Not Started
- Implementation: Not Started
- Specification: Not Started
Support covariant return types. Specifically, allow an overriding method to have a more derived reference type than the method it overrides.
It is a common pattern in code that different method names have to be invented to work around the language constraint that overrides must return the same type as the overridden method. See below for an example from the Roslyn code base.
Support covariant return types. Specifically, allow an overriding method to have a more derived reference type than the method it overrides. This would apply to methods and properties, and be supported in classes and interfaces.
This would be useful in the factory pattern. For example, in the Roslyn code base we would have
class Compilation ...
{
virtual Compilation WithOptions(Options options)...
}
class CSharpCompilation : Compilation
{
override CSharpCompilation WithOptions(Options options)...
}
The implementation of this would be for the compiler to emit the overriding method as a "new" virtual method that hides the base class method, along with a bridge method that implements the base class method with a call to the derived class method.
- Every language change must pay for itself.
- We should ensure that the performance is reasonable, even in the case of deep inheritance hierarchies
- We should ensure that artifacts of the translation strategy do not affect language semantics, even when consuming new IL from old compilers.
We could relax the language rules slightly to allow, in source,
abstract class Cloneable
{
public abstract Cloneable Clone();
}
class Digit : Cloneable
{
public override Cloneable Clone()
{
return this.Clone();
}
public new Digit Clone() // Error: 'Digit' already defines a member called 'Clone' with the same parameter types
{
return this;
}
}
- How will APIs that have been compiled to use this feature work in older versions of the language?
None yet. There has been some discussion at dotnet/roslyn#357