- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Implicit method not considered if the method for which the type would have been converted has a type parameter and a not applicable method with the same exists ...
Tue, 2011-10-04, 21:05
... is this a compiler bug or as specified?
This question arose from the following SO question:
http://stackoverflow.com/questions/7649517/why-is-the-implicit-conversio...
Kipton Barros was able to minimize it and I'll just quote him because he explains it much better than me:
---------------------------------------------------------------------------
It works if I do one or both of the following:
- I choose another method name, like **
- I remove the type parameter
Both things are not really an option to me, so if someone has an idea for a workaround or identifies this as a bug, I would be very grateful!
Thanks,
Simon
This question arose from the following SO question:
http://stackoverflow.com/questions/7649517/why-is-the-implicit-conversio...
Kipton Barros was able to minimize it and I'll just quote him because he explains it much better than me:
---------------------------------------------------------------------------
object Units {
case class Quantity[T: Numeric](value: T) {
def *[M](m: Quantity[T]) = // type M can't be inferred below
Quantity[T](implicitly[Numeric[T]].times(value, m.value))
}
implicit def measure[T: Numeric](v: T) = Quantity[T](v)
val length0 = measure(5) * Quantity(5) // works
val length1 = 5 * Quantity(5) // doesn't work
}For some reason, the conversion
measure
isn't being found because of the type parameterM
on the method*
. If the type parameter is removed from*
, things compile fine. Maybe someone else can explain why?
---------------------------------------------------------------------------
It works if I do one or both of the following:
- I choose another method name, like **
- I remove the type parameter
Both things are not really an option to me, so if someone has an idea for a workaround or identifies this as a bug, I would be very grateful!
Thanks,
Simon