我有一个演员
import akka.actor.{Props, Actor}
import scala.reflect.ClassTag
class MyActor1[T<: Actor: ClassTag] extends Actor {
//....
}
import akka.actor.{Props, Actor}
import scala.reflect.ClassTag
class MyActor2[T <: Actor: ClassTag] extends Actor {
def receive = {
case Start =>
val actor1 = context actorOf Props[MyActor3] //MyActor3 is another actor
actor1 ! Get
case Result(ids: List[Int]) =>
val myActor1List = ids map { new MyActor1[T](_) }
myActor1List foreach { _ ! SomeMessage } // error: "!" is not a member of MyActor1[T]
}
}
错误为"!" is not a member of MyActor1[T]
答案 0 :(得分:3)
这是因为!
是ActorRef
的成员,而不是Actor
。您应该始终通过ActorRef
访问演员,而不是通过直接扩展Actor
的类的实例。
您的myActor1List
包含类MyActor1
的实例,而不是ActorRef
。通过调用context.actorOf[MyActor1]
来创建你的角色,而不是直接实例化MyActor1
个对象。
您已经在自己的代码中执行此操作。请注意:
之间的区别// Create an actor and get an ActorRef
val actor1 = context actorOf Props[MyActor3]
// Create instances of your actor class directly
val myActor1List = ids map { new MyActor1[T](_) }