Why is Scala an implicit permission failure for an overloaded method with a type parameter?

The first example successfully finds an implicit conversion to the foo(String) method, however, as soon as I add a type parameter (see fails ), compilers no longer allow it:

 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 } 

This behavior is for Scala 2.11.7 and 2.12.0-M3.

The implicits documentation does not seem to cover this, and I did not find this particular case in stackoverflow.

Note that my goal is to overload the foo method - if I rename it, the compiler will find it.

http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html

+7
scala type-conversion implicits
source share
1 answer

Both cases seem to fall under this case specification :

Representations apply in three situations:

...

In the choice of em(args) with e type T , if the selector m denotes some member of T , but none of these elements apply to args arguments. In this case, a search is performed for v , which is applicable to e and the result of which contains the method m , which is applicable to args . The search continues, as in the case of implicit parameters, where the implicit region is T If such a representation is found, the choice of em converted to v(e).m(args) .

So that should work. I was actually surprised to see this, because I had not come across a working case before and assumed that there is no implicit search if T has any members named m . I quickly looked through http://issues.scala-lang.org/ , but could not find the corresponding problem.

+1
source share

All Articles