I am at the moment writing a repository actor that works similar to the usual List collection but without shifting elements one position to the left on removal. Hence the use of an array. The only types that this repository will tolerate are those that extend both an Actor and the Indexable type. I want to make use of a Type Alias as a shortcut for this type condition. How do I cast a Type Alias to a generic type inside a class declaration without using a package object as demonstrated below? (See Line #2 and Line #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 }
}
}
}
How about declaring the type alias in the companion object:
import akka.actor.{Props, Actor}
import IndexableRepository._
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 }
}
}
object IndexableRepository {
type Mob = Actor with Indexable
def create[T <: Mob](capacity : Int) = Props(new IndexableRepository[T](capacity))
}
Though, I think Actor
instances are not meant to be referenced anywhere. Storing them in a collection looks somewhat suspicious.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.