【发布时间】:2017-03-06 22:36:55
【问题描述】:
我正在为所有类型解析的伴随对象中定义隐式,除了 AnyVals,如 Long 和 Double 等。我不确定为什么会这样? AnyVals 有不同的解析规则吗?
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]
【问题讨论】: