【问题标题】:Tell is not a member of actor泰尔不是actor的成员
【发布时间】:2013-12-13 20:44:17
【问题描述】:

我有一个演员

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]

【问题讨论】:

    标签: scala akka


    【解决方案1】:

    那是因为!ActorRef 的成员,而不是Actor。您应该始终通过ActorRefs 访问actor,而不是通过直接扩展Actor 的类的实例。

    您的myActor1List 包含类MyActor1 的实例,而不是ActorRefs。通过调用 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](_) }
    

    【讨论】:

    • val myActor1List = ids map { context actorOf Props[MyActor3] } ? T呢?
    • 注意,您可以使用Props 传递id(这就是您要映射的内容),如下所示:ids map { context actorOf Props(classOf[MyActor3], _) }。不知道T。为什么会出现问题?
    • 问题是我必须创建new MyActor1[T](_) 而不仅仅是new MyActor1(_)
    • 然后使用ids map { id =&gt; context actorOf Props(new MyActor1[T](id)) }Props 允许你为你的actor对象提供工厂块。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2021-12-04
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多