question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Resolve method call with duplicate interface method definitions

See original GitHub issue

Background

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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
svickcommented, Aug 19, 2020

These duplicated methods definitions can only be implemented by a single method, in an implementing class.

That’s not true, consider the following generic implementation:

class Map<T, U> : IMap<T, U>
{
    public bool Contains(T val) => true;
    public bool Contains(U val) => false;
}

Usage:

IMap<int,int> map = new Map<int, int>();
// map.Contains(42); // still CS0121

void M<T, U>(IMap<T, U> map)
{
    Console.WriteLine(map.Contains(default(T))); // true
    Console.WriteLine(map.Contains(default(U))); // false
}

M(map);
0reactions
UpperAvenuecommented, Aug 19, 2020

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found