Aladdin - Scala Bugtracking
[#628] project: compiler priority: low category: bug
submitter assigned to status date submitted
Lex Martin fixed 2006-06-19 14:30:57.0
subject implicit conversion of (val * something)
code
object Test {
  abstract class Unit
  object NoUnit extends Unit
  object Hour extends Unit

  case class Measure(scalar: Double, unit: Unit) {
    def *(newUnit: Unit) = Measure(scalar, newUnit)
  }

  implicit def double2Measure(scalar: Double) =
    Measure(scalar, NoUnit)


  def main(args: Array[String]): scala.Unit = {
    Console.println("3.0 * Hour = " + (3.0 * Hour))
  }
}
what happened
/home/lex/eclipse/workspace/scala2/test/files/run/impconvtimes.scala:15 error: overloaded method value * wi\
th alternatives (scala.Double)scala.Double  (scala.Float)scala.Double  (scala.Long)scala.Double  \
(scala.Int)scala.Double  (scala.Char)scala.Double  (scala.Short)scala.Double  (scala.Byte)scala.D\
ouble cannot be applied to (Test.this.Hour.type)
    Console.println("3.0 * Hour = " + (3.0 * Hour))
                                           ^
one error found
what expected It would be nice if this worked, although granted it is a corner case. The value could be converted uing double2Measure. If you change every "*" to "***", then it does work. The question is: do implicit conversions of the receiver happen when there is (1) no method with the correct name, or (2) no type-compatible method with the correct name. It would be nice if it was (2).
[back to overview]
Changes of this bug report
Lex  edited on  2006-06-19 14:31:10.0
Martin  edited on  2006-06-21 16:03:24.0
It's (2). The same case worked for Bigints. This example failed because of a tiny technical reason: the system was looking for a method *: Hour => ?. The * method in Measure did not qualify because it takes a measure, not an Hour. This has been fixed: we now look for a method * whose parameter type is an arbitrary supertype of Hour.