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.

Implicit resolution fails for overloaded method with type parameter

See original GitHub issue

Dotty seems to have inherited this issue from Scala: https://github.com/scala/bug/issues/9523

object works {
  class A {
    def foo(): String = ???
  }
  implicit class PimpedA(a: A) {
    def foo(i: String): String = ???
  }
  val a = new A()
  a.foo("test") //compiles
}

object fails { //same as `works`, but adds type parameter
  class A {
    def foo[T](): String = ???
  }
  implicit class PimpedA(a: A) {
    def foo[T](i: String): String = ???
  }
  val a = new A()
  PimpedA(a).foo("test") // compiles
  a.foo("test") // error: too many arguments for method foo: (): String
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
oderskycommented, Mar 10, 2020

The attempted fix in scalac is still not merged.

The best summary is from @adriaanm’ on scala/scala#7396:

The spec says:

In a selection e.m(args) with e of type T, if the selector m denotes some member(s) of T, but none of these members is applicable to the arguments args.

In this case a view v is searched which is applicable to e and whose result contains a method m which is applicable to args. The search proceeds as in the case of implicit parameters, where the implicit scope is the one of T. If such a view is found, the selection e.m is converted to v(e).m(args).

The problem was that the failing selection would actually look like e.m[T], where we need to strip the [T] so that the adapted expression v(e).m’ can infer the appropriate type params (most likely different from [T]).

So, one can argue that the compiler and the spec actually agree: It’s not a selection e.m(args) but a selection e.m[T](args). Of course the [T] is inserted by type inference, so this now depends on the way rules in the spec are ordered.

To fix this on the typer side would be very messy, I fear. This is also born out by the fact that the attepted fix in scalac did not make it in so far. So I prefer to declare the status quo as law and fix it on the spec side.

0reactions
smartercommented, Jun 7, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Implicit resolution fails for overloaded method with type ...
The first example successfully finds the implicit conversion to the method foo(String), however as soon as I add a type parameter (see fails)...
Read more >
Why does Scala implicit resolution fail for ... - Stack Overflow
The first example successfully finds the implicit conversion to the method foo(String) , however as soon as I add a type parameter (see...
Read more >
Why does Scala implicit resolution fail for overloaded method ...
In a selection e.m(args) with e of type T , if the selector m denotes some member(s) of T , but none of...
Read more >
How does overload resolution interact with implicit conversions?
I failed to find information on the role of implicit conversions in overload resolution. Is there a way to make the compiler try...
Read more >
Overload resolution failed because no accessible '<method ...
You have made a call to an overloaded method, but the compiler has found two or more overloads with parameter lists to which...
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