-
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
Proposal: Explicit overriding of virtual methods #1618
Comments
Reformatted for readability. Before (C# 7.x): public interface IAnimal
{
public IAnimal GiveBirth();
}
public class Cat : IAnimal
{
IAnimal IAnimal.GiveBirth() =>
GiveBirth();
public Cat GiveBirth() => new Cat();
} Future (C# X.0): public abstract class Animal
{
public abstract Animal GiveBirth();
}
public class Cat : Animal
{
Animal base.GiveBirth() => GiveBirth();
public Cat GiveBirth() => new Cat();
} Did you mean to use an Using Also, no language feature can only be a stopgap. Whatever is done will need to be supported until C#'s dying days to maintain backwards compatibility. |
yaakov-h: I have updated my proposal to explain it better. |
Design3 of my exploration of proposals for covariant return types in C# discusses this proposal in great detail, including it's implementation, specification, advantages and disadvantages. |
A few explorable areas:
In addition to making the override syntax stand out with
I regard the main objective of explicit overriding is to override a member and hide it with a new one. Now suppose a class hides a member from the base class by using If we decide that an explicitly overridden member cannot be overridden in further-derived class, we should mark it as If we decide that an explicitly overridden member can be overridden again in further-derived class (unless |
@YairHalberstadt given that covariant return types are happening, do we still need this? |
Currently there's a proposal relating to covariant return types: #49
However it doesn't seem to be going anywhere very fast, so I would like to propose a different workaround.
At the moment you can create something that behaves like a covariant return types with interfaces as follows:
This doesn't work however when inheriting classes, since you cannot explicitly override the base. I propose allowing similar syntax when overriding a virtual method;
Then you could use it as follows
This will feel like covariant return types to anyone who consumes Cat.
There may be other uses, when someone wants to inherit from a class but hide their methods (similar to explicit interface implementation), although I cannot think of one right now.
The text was updated successfully, but these errors were encountered: