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
scala type-conversion implicits
Michael pollmeier
source share