只是想知道以下是错误还是功能:
Welcome to Scala version 2.10.0-M3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class TypeClass[T]
defined class TypeClass
scala> trait A[T] {
| implicit val t = implicitly[TypeClass[T]]
| }
<console>:9: error: could not find implicit value for parameter e: TypeClass[T]
implicit val t = implicitly[TypeClass[T]]
^
正如预期的那样,这不会编译,因为T
. 但是当我添加类型注释时,它会编译:
scala> trait A[T] {
| implicit val t: TypeClass[T] = implicitly[TypeClass[T]]
| }
defined trait A
编译器不应该在这里抱怨吗?为什么类型注释应该有所作为?如果我们用这个特征实例化一些东西,t
就是null
。