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.

Interface Overload Generic Member - Compiler error when removing overload

See original GitHub issue

Bug Report

Removing an overload member Controller.handle from Controller causes a compilation error.

🔎 Search Terms

  • Interface overload
  • Member Type Parameter
  • Works When Removing overload

⏯ Playground Link

Playground

💻 Code

Code below compiles

export interface Controller {
  handle<T>(httpRequest: HttpRequest<T>): HttpResponse<T>;
  handle<TRequest, TResponse>(httpRequest: HttpRequest<TRequest>): HttpResponse<TResponse>;
}

Code below does not compile

export interface Controller {
  // handle<T>(httpRequest: HttpRequest<T>): HttpResponse<T>;
  handle<TRequest, TResponse>(httpRequest: HttpRequest<TRequest>): HttpResponse<TResponse>;
}

Code below does not compile

export interface Controller {
  handle<T>(httpRequest: HttpRequest<T>): HttpResponse<T>;
  // handle<TRequest, TResponse>(httpRequest: HttpRequest<TRequest>): HttpResponse<TResponse>;
}

🙁 Actual behavior

Code does not compile when removing any of the overloads.

🙂 Expected behavior

Since having an overload in Controller.handle works, having just one member with the correct signature should also work.

Additional Info

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinJohnscommented, Nov 1, 2022

Your interface says: This interface provides this generic method.
Your class implements no such generic method.

Simplified, you try this:

interface A { handle<T>(): T }
class E implements A { handle(): string { return "abc"; } }

The interface allows me to call handle with arbitrary generic arguments. The class ignores this, and just returns a string. So when calling the method I end up with nonsense:

const a: A = new E();
const r: number = a.handle<number>(); // Actually a string

When adding an overload to this example it will compile just fine as well, like your example. Playground link

0reactions
RyanCavanaughcommented, Nov 3, 2022

I don’t recall offhand what the assignability rules are around overload targets. I would have said “assignable to each” but that’s clearly not what’s going on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Generic Interface method-overload - Stack Overflow
But now I'm getting a compiler error i dont understand: java:15: error: name clash: insert(T,int) and insert(C, ...
Read more >
Overload resolution failed because no accessible ...
A call to an overloaded generic procedure cannot be resolved because the compiler cannot access any overloaded version with the appropriate ...
Read more >
Overloading and generic constraints - Jon Skeet's coding blog
So, the first method is rejected, the second is fine, so it compiles with no problems. The important thing to note is that...
Read more >
Chapter 8. Classes - Oracle Help Center
Like methods, they may be overloaded (§8.8.8). ... It is a compile-time error if an inner class declares a member that is explicitly...
Read more >
Overload resolution - Kotlin language specification
Non-extension member callables named f of type AN . If at least two of these sets are non-empty, this is a compile-time error....
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