Proposal: Resolve method call with duplicate interface method definitions
See original GitHub issueBackground
Consider an interface with multiple generic type parameters and that defines overloaded methods that differ only by the generic type used as the type of a parameter. When the same type is used as the generic type arguments, these overloaded methods definitions are essentially duplicated. These duplicated methods definitions can only be implemented by a single method, in an implementing class.
Example:
Interface:
interface IMap<T,U>
{
bool Contains(T val);
bool Contains(U val);
}
Implementation:
class Map<T> : IMap<T,T>
{
public bool Contains(T val) => true;
}
Currently, when calling Contains on a IMap<T,T>, the error CS0121 for ambiguous method call is raised.
IMap<int,int> map = new Map<int>();
...
map.Contains(42); // CS0121
Proposal
Since these duplicate interface method definitions can only resolve to a single method, the calling method should resolve to the last defined method that matches in the interface.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
proposal: allow duplicate methods in interfaces #15666
This is a proposal for a minor, backwards-compatible language change. Currently, all methods in an interface type must have different names.
Read more >Java duplicate method when implement - interface
1) create an abstract for all these classes and put "go2" inside? or. 2) create "go2" in each class that and call "super()"?...
Read more >Static and non-static interface member calls in .NET
First, an interface call is not really the same as a virtual method call. Interface calls are implemented using virtual stub dispatch (VSD ......
Read more >Default interface methods - C# 8.0 draft feature specifications
Default interface methods enable an API author to add methods to an ... Open questions are called out throughout the proposal, above.
Read more >Java 8 Interface Changes - static method, default method
One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That’s not true, consider the following generic implementation:
Usage:
@svick I see what you’re saying now. You’re right, we wouldn’t able to resolve the method at compile time in your example. Thanks for taking the time!