“自我”如何实现?在 Scala 演员中工作?

发布于 2024-12-05 12:24:57 字数 964 浏览 0 评论 0原文

以下代码片段摘自《Scala

import actors.Actor

object NameResolver extends Actor {

 import java.net.{InetAddress, UnknownHostException}


 def act() {
   react {
     case (name: String, actor: Actor) =>
       actor ! getIp(name)
       act()
     case "EXIT" =>
       println("Name resolver exiting.")
     // quit
     case msg =>
       println("Unhandled message: " + msg)
       act()
   }
 }

 def getIp(name: String): Option[InetAddress] = {
   try {
     Some(InetAddress.getByName(name))
   } catch {
     case _: UnknownHostException => None
   }
 }
}

First 编程》,在 React {} 中,对 act() 的递归调用会做什么?看起来所有的案例都会失败,并且只会什么也不做,直到最后。

其次,在书中,他们使用了以下 REPL 示例

NameResolver ! ("www.scala-lang.org", self)

“自我”从何而来?我尝试在主要方法中复制此内容

 def main(args: Array[String]) {
   NameResolver.start()
   NameResolver ! ("www.scala-lang.org", Actor.self)
 }

以上不起作用

The following code snippet is taken from Programming in Scala

import actors.Actor

object NameResolver extends Actor {

 import java.net.{InetAddress, UnknownHostException}


 def act() {
   react {
     case (name: String, actor: Actor) =>
       actor ! getIp(name)
       act()
     case "EXIT" =>
       println("Name resolver exiting.")
     // quit
     case msg =>
       println("Unhandled message: " + msg)
       act()
   }
 }

 def getIp(name: String): Option[InetAddress] = {
   try {
     Some(InetAddress.getByName(name))
   } catch {
     case _: UnknownHostException => None
   }
 }
}

Firstly within react {} what does the recursive call to act() do? It looks like all the cases would fail and it will simply drop through to the end doing nothing.

Secondly in the book, they use the following REPL example

NameResolver ! ("www.scala-lang.org", self)

Where does 'self' come from? I have tried to replicate this in a main method

 def main(args: Array[String]) {
   NameResolver.start()
   NameResolver ! ("www.scala-lang.org", Actor.self)
 }

The above does not work

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

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

发布评论

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

评论(2

葬花如无物 2024-12-12 12:24:57
  1. act() 递归运行,除非您发送 EXIT 消息。对于任何其他既不匹配 EXIT 也不匹配 (name: String, actor: Actor) 的消息,将使用 case msg 。换句话说,case msg 是一种处理所有不匹配消息的通用处理程序。

  2. Actor.self 是一个 从演员体内引用演员实例的可靠方式(建议使用self 而不是 this,以引用正确的实例)。在您的情况下,您不是从演员调用 Actor.self ,因此它会失败。

  1. act() runs recursively, unless you send an EXIT message. For any other message that neither match EXIT nor (name: String, actor: Actor), case msg will be used. In other words case msg is a kind of a generic handler that processes all the unmatched messages.

  2. Actor.self is a reliable way to refer to the actor instance from within an actor's body (it's recommended to use self instead of this, to refer to the right instance). In your case, you're calling Actor.self not from an actor, and, thus, it fails.

强辩 2024-12-12 12:24:57
  1. 仅当消息可用时才会调用react内的代码。因此,如果您在 case 子句中递归调用 act(),actor 将“等待”直到发送新消息(不会阻塞线程)。当您想要根据 Actor 收到的内容来更改 Actor 的行为时,它是循环的替代方案。在这里,当您收到“EXIT”时,您将停止等待消息。

  2. self 应该在每次想要使用 this 时在 Actor 的内部使用。

  1. The code inside react is called only when a new message is available. Thus, if you call recursively act() inside a case clause, the actor will "wait" until a new message is sent (without blocking a thread). It's an alternative to loop when you want to change the behavior of the actor according to what it received. Here you stop waiting for messages when you get "EXIT".

  2. self is supposed to be used inside an actor everytime you want to use this.

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