Scala 中的并发参与者和特性
各位, 我是 Scala 新手,正在尝试解决一些问题。我一直在搞乱一些特质,我真的很喜欢它们“混合”功能和界面的能力。我也一直在研究并发和 Actor,我真的很喜欢能够轻松地对高度复杂的并发系统进行建模的能力。
我遇到的问题是我无法找到结合这两个世界的模式。我真正想要的是使用特征来确定 Actor 响应哪些类型的消息,从而允许跨继承层次结构进行不同的响应。
因此,以战场模拟器为例:我有模拟器,它们是特征。战场上的所有物体都是模拟物,模拟物应该通过发送“Pong”来响应“Ping”——就是这样。我想要一个 IFF
特征,它允许模拟物将自己标识为消息发送者的朋友或敌人。另一个特征应该是移动
,这意味着模拟物可以移动并且应该响应告诉模拟物其新目的地的消息。
正如你所看到的,我可能有: class Tank extends Actor with Simulant with IFF with Mobile
,但我可能有类似屏障的东西,例如 class Barrier extends Actor with Simulant
。
我还无法做的是创建 act()
方法、循环、反应等的正确组合以使这种情况成为可能。简而言之,是否可以“混合消息反应器”,或者 Scala 是否限制我选择具有单一继承的 Actor 或没有 Actor 的 mixins?
谢谢!
Folks,
I'm new to Scala and am trying to figure something out. I've been messing around a bit with traits and I really like their ability to "mix in" functionality and interface. I've also been messing around with concurrency and Actors and I really like the ability I get to model highly complicated concurrent systems easily.
The problem I'm having is I can't quite find a pattern for combining both worlds. What I'm really looking for is using traits to determine which types of messages an Actor responds to, allowing for different responses across inheritance hierarchies.
So, to use a battlefield simulator example: I have Simulants, which are traits. All objects on the battlefield are Simulants, and Simulants should respond to "Ping" by sending "Pong" - this is it. I want an IFF
trait, which will allow a simulant to identify itself as a friend or foe of the message sender. Another trait should be Mobile
, which means the simulant can move and should respond to messages telling the simulant its new destination.
As you can see, I might have : class Tank extends Actor with Simulant with IFF with Mobile
, but I might have something like a barrier, e.g. class Barrier extends Actor with Simulant
.
What I have not yet been able to do is create the right combination of act()
methods, loops, reacts, and so forth to make this scenario possible. In short, is it possible to "mix in message reactors" or does Scala limit me to choosing Actors with single inheritance or mixins without actors?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这又如何呢?
编辑:我刚刚尝试过这个,它对我来说编译得很好:
另一方面,它似乎实际上不起作用:当我替换
case Ping =>; println("pinged")
,我没有看到它:What about this?
EDIT: I've just tried this and it compiles fine for me:
On the other hand, it doesn't seem to actually work: when I replace
case Ping => println("pinged")
, I don't see it:我写了一篇关于这个问题的博客文章以及我找到的解决该问题的折衷方案。
http://www.kotancode。 com/2011/07/19/traits-multiple-inheritance-and-actors-in-scala/
I did a blog post up on this problem and the compromise that I've found for solving the problem.
http://www.kotancode.com/2011/07/19/traits-multiple-inheritance-and-actors-in-scala/