class Test1(buf:Buffer[AnyRef])
class Test2(buf:Buffer[String]) extends Test(buf) 编译器错误:
type mismatch;
found : scala.collection.mutable.Buffer[String]
required: scala.collection.mutable.Buffer[Any]
Note: org.msgpack.type.Value <: Any, but trait Buffer is invariant in type A. You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)发布于 2013-06-19 18:02:05
简而言之:不能向Buffer[String]添加AnyRef
val b: Buffer[AnyRef] = Buffer[String]()
b += new Object // ???Buffer[String]不能为Buffer[AnyRef],因为Buffer[T]在类型参数T上不是协变的。不能将其声明为协变(Buffer[+T]),因为在逆变位置使用了T (例如,在+=方法中)。
https://stackoverflow.com/questions/17187916
复制相似问题