Skip to content

Implicit resolution fails for overloaded method with type parameter #3769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jasper-M opened this issue Jan 5, 2018 · 5 comments
Closed

Comments

@Jasper-M
Copy link
Contributor

Jasper-M commented Jan 5, 2018

Dotty seems to have inherited this issue from Scala: scala/bug#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
}
@SethTisue
Copy link
Member

@odersky this is the bug that @adriaanm and @szeiger were telling you about today

@smarter
Copy link
Member

smarter commented Nov 8, 2018

Fix in scalac for the same bug: scala/scala#7396

@odersky
Copy link
Contributor

odersky commented 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.

@mdedetrich
Copy link

mdedetrich commented Nov 26, 2020

I understand that the fix is messy but this also appears like a massive oversight from a language usage perspective, it seems completely arbitrary that extension methods work without type parameters but fail with them.

Also if the spec is unclear when it comes to ordering then we should try and fix the spec so it is clear rather than trying to use the current ambiguity as a justification to not fix an issue because its inconvenient to do so.

@smarter
Copy link
Member

smarter commented Jun 7, 2021

Fixed by #12719

@smarter smarter closed this as completed Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants