Tell不是演员的成员

时间:2013-12-13 12:30:53

标签: scala akka

我有一个演员

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]

1 个答案:

答案 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](_) }