在地图中实例化并启动 Scala Actor
我正在尝试一张演员地图,想知道如何实例化它们并一下子启动它们......
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
start
的问题是它不知道你的 actor 的真实类型。因此,它返回一个通用的。为了解决这个问题,您需要一种紧凑的方式来启动它并仍然返回您实际拥有的演员(不是超类)。实际上,这听起来像是一个有用的功能,不是吗?——获取一个对象,让它做某事,然后返回该对象?现在您可以
并且类型将被保留。 (如果您不使用 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?Now you can
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?)
这个怎么样:
How about this: