Allow methods and/or properties with a more restrictive type to implement interface methods and/or properties. #8009
Unanswered
morfanaion
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
See: #4372 |
Beta Was this translation helpful? Give feedback.
1 reply
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.
Uh oh!
There was an error while loading. Please reload this page.
-
When overriding a virtual method of a base class you are allowed to specify a more restrictive return type (so a return type that has the original return type of one of its base types). For example:
The fact that this is allowed sort of makes sense. DerivativeClassForTesting will always be castable to BaseClassForTesting and as such, this would not lead to a problem per se. However, the same logic does not seem to apply to interfaces. If I try to do the same with interfaces I have to do this:
In my use cases I have a library that contains the interfaces of my implementations where the implementations are in a different library. This method is done to allow other libraries to be able to handle my objects and me being able to use objects of those other libraries, without running into circular referencing. This means that I can use the actual interfaces of the other libraries in my implementations and vice versa, but the interfaces themselves have to use base interfaces from a shared library. In my implementations I will often prefer to use the return types of the actual interfaces I'm referencing, mainly for use within my own library, which forces me to do explicit implementations. Those explicit implementations in these case are relatively redundant as all they will ever do is just reference the method that returns the derivative.
As such, I would request a change that would allow a method or property with a type that is a derivative of the type of the method or property in the interface to implement that method or property, so that we can reduce the number of these explicit implementations.
[EDIT]new to this, needed to fix the code blocks :-)[/EDIT]
Beta Was this translation helpful? Give feedback.
All reactions