在地图中实例化并启动 Scala Actor

发布于 2024-10-11 02:46:45 字数 1215 浏览 4 评论 0原文

我正在尝试一张演员地图,想知道如何实例化它们并一下子启动它们......

import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.mutable._

abstract class Message
case class Update extends Message

object Test {
    val groupings = "group1" :: "group2" :: "group3":: Nil
    val myActorMap = new HashMap[String,MyActor]

    def main(args : Array[String]) {
        groupings.foreach(group => myActorMap += (group -> new MyActor))
        myActorMap("group2").start
        myActorMap("group2") ! Update
    }
}

class MyActor extends Actor {
    def act() {
        loop {
            react {
                case Update =>
                    println("Received Update")
                case _ =>
                    println("Ignoring event")
            }
        }
    }   
}

台词:

    myActorMap("group2").start

将抓住第二个实例,让我启动它,但我想能够做类似的事情:

        groupings.foreach(group => myActorMap += (group -> (new MyActor).start))

但无论我如何包装新的 Actor,编译器都会抱怨以下内容:

类型不匹配;找到:scala.actors.Actor 需要: com.myCompany.test.MyActor

或各种其他投诉。我知道匿名类一定很简单,但我现在看不到。有什么建议吗?提前致谢!!

I'm experimenting with a map of actors, and would like to know how to instantiate them and start them in one fell swoop...

import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.mutable._

abstract class Message
case class Update extends Message

object Test {
    val groupings = "group1" :: "group2" :: "group3":: Nil
    val myActorMap = new HashMap[String,MyActor]

    def main(args : Array[String]) {
        groupings.foreach(group => myActorMap += (group -> new MyActor))
        myActorMap("group2").start
        myActorMap("group2") ! Update
    }
}

class MyActor extends Actor {
    def act() {
        loop {
            react {
                case Update =>
                    println("Received Update")
                case _ =>
                    println("Ignoring event")
            }
        }
    }   
}

The line:

    myActorMap("group2").start

will grab the second instance, and let me start it, but I would like to be able to do something more like:

        groupings.foreach(group => myActorMap += (group -> (new MyActor).start))

but no matter how I wrap the new Actor, the compiler complains with something along the lines of:

type mismatch; found : scala.actors.Actor required:
com.myCompany.test.MyActor

or various other complaints. I know it must be something simple to do with anonymous classes, but I can't see it right now. Any suggestions? Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-10-18 02:46:45

start 的问题是它不知道你的 actor 的真实类型。因此,它返回一个通用的。为了解决这个问题,您需要一种紧凑的方式来启动它并仍然返回您实际拥有的演员(不是超类)。实际上,这听起来像是一个有用的功能,不是吗?——获取一个对象,让它做某事,然后返回该对象?

class SideEffector[A](a: A) {
  def effect(f: A => Unit) = { f(a); a }
}
implicit def everythingHasSideEffects[A](a: A) = new SideEffector(a)

现在您可以

(new MyActor).effect(_.start)

并且类型将被保留。 (如果您不使用 Scalaz,这种功能通常非常有用,您可能想将其放入您个人的方便代码片段库中。它在我的库中。您有一个,不是吗?)

The problem with start is that it doesn't know the true type of your actor. Thus, it returns a generic one. To get around this, you need a compact way to start it and still return the actor you actually have (not a superclass). Actually, that sounds like a useful capability in general, doesn't it?--to take an object, have it do something, and then return the object?

class SideEffector[A](a: A) {
  def effect(f: A => Unit) = { f(a); a }
}
implicit def everythingHasSideEffects[A](a: A) = new SideEffector(a)

Now you can

(new MyActor).effect(_.start)

and the type will be preserved. (If you're not using Scalaz, this sort of capability is so useful in general that you may want to drop it into your personal library of handy code snippets. It's in mine. You have one, don't you?)

失退 2024-10-18 02:46:45

这个怎么样:

    def main(args : Array[String]) {
      groupings.foreach {
        group =>
        val actor = new MyActor
        actor.start
        myActorMap += (group -> actor)
      }

      myActorMap("group2") ! Update
    }

How about this:

    def main(args : Array[String]) {
      groupings.foreach {
        group =>
        val actor = new MyActor
        actor.start
        myActorMap += (group -> actor)
      }

      myActorMap("group2") ! Update
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文