Scala 中的并发参与者和特性

发布于 2024-11-23 16:39:21 字数 691 浏览 1 评论 0原文

各位, 我是 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 技术交流群。

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

发布评论

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

评论(2

雪若未夕 2024-11-30 16:39:21

这又如何呢?

trait Simulant {
  def body: PartialFunction[Any, Unit] = {
    case Ping => reply(Pong)
  }

  def act = loop { react(body) }
}

trait IFF extends Simulant {
  override def body = super.body orElse {
    case FriendOrFoe => ...
  }
}

trait Mobile extends Simulant {
  override def body = super.body orElse {
    case Move(direction) => ...
  }
}

编辑:我刚刚尝试过这个,它对我来说编译得很好:

aromanov@alexey-desktop:~$ scala
Welcome to Scala version 2.9.0.1 (OpenJDK Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object A {
     | import scala.actors.Actor
     | trait Simulant extends Actor {
     |   def body: PartialFunction[Any, Unit] = {
     |     case Ping => reply(Pong)
     |   }
     | 
     |   def act = loop { react(body) }
     | }
     | 
     | trait IFF extends Simulant {
     |   override def body = super.body orElse {
     |     case FriendOrFoe => {}  
     |   }
     | }
     | 
     | trait Mobile extends Simulant {
     |   override def body = super.body orElse {
     |     case Move(direction) => {}
     |   }
     | }
     | 
     | class Foo extends Actor with Simulant with IFF with Mobile
     | 
     | object Ping
     | object Pong
     | object FriendOrFoe
     | case class Move(direction: Int)
     | }
defined module A

另一方面,它似乎实际上不起作用:当我替换 case Ping =>; println("pinged"),我没有看到它:

scala> (new A.Foo) ! A.Ping

scala>

What about this?

trait Simulant {
  def body: PartialFunction[Any, Unit] = {
    case Ping => reply(Pong)
  }

  def act = loop { react(body) }
}

trait IFF extends Simulant {
  override def body = super.body orElse {
    case FriendOrFoe => ...
  }
}

trait Mobile extends Simulant {
  override def body = super.body orElse {
    case Move(direction) => ...
  }
}

EDIT: I've just tried this and it compiles fine for me:

aromanov@alexey-desktop:~$ scala
Welcome to Scala version 2.9.0.1 (OpenJDK Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object A {
     | import scala.actors.Actor
     | trait Simulant extends Actor {
     |   def body: PartialFunction[Any, Unit] = {
     |     case Ping => reply(Pong)
     |   }
     | 
     |   def act = loop { react(body) }
     | }
     | 
     | trait IFF extends Simulant {
     |   override def body = super.body orElse {
     |     case FriendOrFoe => {}  
     |   }
     | }
     | 
     | trait Mobile extends Simulant {
     |   override def body = super.body orElse {
     |     case Move(direction) => {}
     |   }
     | }
     | 
     | class Foo extends Actor with Simulant with IFF with Mobile
     | 
     | object Ping
     | object Pong
     | object FriendOrFoe
     | case class Move(direction: Int)
     | }
defined module A

On the other hand, it doesn't seem to actually work: when I replace case Ping => println("pinged"), I don't see it:

scala> (new A.Foo) ! A.Ping

scala>
输什么也不输骨气 2024-11-30 16:39:21

我写了一篇关于这个问题的博客文章以及我找到的解决该问题的折衷方案。

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/

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