The first example successfully finds the implicit conversion to the method foo(String)
, however as soon as I add a type parameter (see fails
) the compiles doesn't resolve it anymore:
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 behaviour is the same for Scala 2.11.7 and 2.12.0-M3.
The documentation on implicits doesn't seem to cover this and I didn't find this exact case on stackoverflow.
Note that my goal is to overload the method foo
- if i rename it, the compiler finds it.
http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html
Implicit parameters are the parameters that are passed to a function with implicit keyword in Scala, which means the values will be taken from the context in which they are called.
What is Scala Method Overloading? Scala Method overloading is when one class has more than one method with the same name but different signature. This means that they may differ in the number of parameters, data types, or both. This makes for optimized code.
Both cases seem to fall under this case of the specification:
Views are applied in three situations:
...
In a selection
e.m(args)
withe
of typeT
, if the selectorm
denotes some member(s) ofT
, but none of these members is applicable to the argumentsargs
. In this case a viewv
is searched which is applicable toe
and whose result contains a methodm
which is applicable toargs
. The search proceeds as in the case of implicit parameters, where the implicit scope is the one ofT
. If such a view is found, the selectione.m
is converted tov(e).m(args)
.
So it should work. I was actually surprised to see it, because I've never run into the working case before and assumed that there is no implicit search if T
has any members named m
. I've taken a quick look at http://issues.scala-lang.org/, but couldn't find a relevant issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With