我定义了伴随对象中的含义,除了AnyVals之外的所有类型(如Long和Double等)都可以解析。我不完全确定它为什么是这种情况? AnyVals有不同的解析规则吗?对于AnyVal,Scala隐式分辨率不同?
class X(val i:Int) {
def add[T](implicit x:SomeType[T])=println(x)
}
object X {
implicit def xx = XType
implicit def ll = LType
implicit def dd = DType
}
object Console {
def main(args: Array[String]): Unit = {
new X(3).add[X] // works fine
new X(3).add[Long] // error Error:(16, 16) could not find implicit value for parameter x: com.x.SomeType[Long]
new X(3).add[Double] // error Error:(16, 16) could not find implicit value for parameter x: com.x.SomeType[Double]
}
}
sealed trait SomeType[T]
case object XType extends SomeType[X]
case object LType extends SomeType[Long]
case object DType extends SomeType[Double]