scala-将asInstanceOf与泛型一起使用
我遇到了泛型的编译问题。当我使用scala-将asInstanceOf与泛型一起使用,scala,generics,casting,type-inference,Scala,Generics,Casting,Type Inference,我遇到了泛型的编译问题。当我使用asInstanceOf时,代码编译得很好。我想摆脱作为的替代品 我还看到了一些与的用法有关的问题,但我没有帮助我 trait RoundRobin[R <: Resource, F[_] <: mutable.ListBuffer[_]] { self: RoundRobin[R, F] => // some public functions private def overrideMutableResourceList(ori
asInstanceOf
时,代码编译得很好。我想摆脱作为
的替代品
我还看到了一些与的用法有关的问题,但我没有帮助我
trait RoundRobin[R <: Resource, F[_] <: mutable.ListBuffer[_]] {
self: RoundRobin[R, F] =>
// some public functions
private def overrideMutableResourceList(original: F[R], updated: F[R]): F[R] = {
val tempPool = original.asInstanceOf[mutable.ListBuffer[R]]
original.indices.foreach(i => {
val e = updated(i).asInstanceOf[R]
tempPool.update(i, e)
})
tempPool.asInstanceOf[F[R]]
}
此问题也发生在[mutable.ListBuffer[R]]
original.asInstanceOf[R]]行中
- 这个问题的原因是什么
- 如何避免使用
的替代作为
谢谢在
F[A]
和ListBuffer[A]
之间没有关系,只有∀A.∃B F[A]
[error] /Users/...../RoundRobin.scala:108: type mismatch;
[error] found : tempPool.type (with underlying type scala.collection.mutable.ListBuffer[R])
[error] required: F[R]
[error] tempPool
[error] ^
[error] one error found
[error] (clustering/compile:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Oct 3, 2017 2:53:34 AM
type ConstLBInt[A] = ListBuffer[Int]
val x: RoundRobin[Resource, ConstLBInt] = ??? // Legal
// Tries to manipulate ListBuffer[Int]s as if they were ListBuffer[Resources]s
trait RoundRobin[R <: Resource, F[A] <: mutable.ListBuffer[A]]
// ! !