The Scala 2.8 spec says in section 7.3 (the highlight is mine):
Implicit parameters and methods can also define implicit conversions called views. The view from type S to type T is determined by an implicit value that has the function type S => T or (=> S) => T or by a method that is converted to a value of this type. Representations apply in two situations.
- If the expression e is of type T and T does not match the expressions, the expected type is pt. In this case, an implicit v is searched, which is applicable to e and , the result of which matches pt. The search continues, as in the case of implicit parameters, where the implicit region is one of T => pt. If this is the case, the expression e is converted to v (e).
[...]
Considering the above and the following facts:
Long not a subtype of java.lang.Comparable[Long] , i.e. does not match for input T , where T <: java.lang.Comaparable[Long]Predef contains implicit def longWrapper (x: Long) : RichLongRichLong is a subtype of java.lang.Comparable[Long] , that is, it matches for input T , where T <: java.lang.Comaparable[Long]
I expect that the implicit conversion will be applied where Long is encountered, and a subtype of java.lang.Comparable[Long] is expected. But:
scala> def test[T <: java.lang.Comparable[Long]](c: T) = println(c) test: [T <: java.lang.Comparable[Long]](c: T)Unit scala> test(12L) <console>:7: error: inferred type arguments [Long] do not conform to method test type parameter bounds [T <: java.lang .Comparable[Long]] test(12L) ^
The result will be as expected if the value is explicitly converted:
scala> test(longWrapper(12L)) 12
Why is the conversion function not applied implicitly?
source share