EDIT - apologies, I only just noticed your first error. There is no way of instantiating a T
at runtime because the type information is lost when your program is compiled (via type erasure)
You will have to pass in some factory to achieve the construction:
class BalanceActor[T <: Actor](val fac: () => T) extends Actor {
val workers: Int = 10
private lazy val actors = new Array[T](workers)
override def start() = {
for (i <- 0 to (workers - 1)) {
actors(i) = fac() //use the factory method to instantiate a T
actors(i).start
}
super.start()
}
}
This might be used with some actor CalcActor
as follows:
val ba = new BalanceActor[CalcActor]( { () => new CalcActor } )
ba.start
As an aside: you can use until
instead of to
:
val size = 10
0 until size //is equivalent to:
0 to (size -1)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…