【发布时间】:2015-05-22 02:15:40
【问题描述】:
我的代码很简单:
class MyClass {
var foo: IndexedSeq[MyClass] = IndexedSeq()
def bar(newValues: MyClass*) = foo = newValues.toArray
}
该类包含一个变量和一个方法。变量 foo 是 IndexSeq 的 MyClass 对象。它还包含一个方法bar,它将Seq 的MyClass 对象作为参数。因为Seq[T]不能分配给IndexSeq[T],因为后者是前者的子类,所以我不得不调用toArray。
使用此代码,编译器报错如下
polymorphic expression cannot be instantiated to expected type;
found : [B >: MyClass]Array[B]
required: IndexedSeq[MyClass]
def bar(newValues: MyClass*) = foo = newValues.toArray
^
于是我找到了解决办法,就是调用toIndexSeq而不是toArray,编译器就不再报错了。
即使问题对我来说已经消失了,我仍然想知道为什么会出现这样的错误。
【问题讨论】:
标签: scala types polymorphism