【发布时间】:2015-09-21 19:27:44
【问题描述】:
我目前正在编写一个存储库actor,它的工作方式类似于通常的 List 集合,但在删除时不会将元素向左移动一个位置。因此使用数组。 此存储库将允许的唯一类型是同时扩展 Actor 和 Indexable 类型的类型。我想使用类型别名作为这种类型条件的快捷方式。如何在不使用包对象的情况下将类型别名转换为类声明中的泛型类型,如下所示? (参见第 2 行和第 7 行)
package object model {
type Mob = Actor with Indexable
object IndexableRepository {
def create[T <: Mob](capacity : Int) = Props(new IndexableRepository[T](capacity))
}
final class IndexableRepository[T <: Mob](val capacity : Int) extends Actor {
private var indexables = new Array[Mob](capacity)
def receive : Actor.Receive = {
case _ => unhandled()
}
private def findAvailable(indexables : Seq[Mob]) = {
(0 to indexables.size) find { idx => indexables(idx) == null }
}
}
}
【问题讨论】:
标签: scala generics akka type-alias