“自我”如何实现?在 Scala 演员中工作?
以下代码片段摘自《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
act()
递归运行,除非您发送EXIT
消息。对于任何其他既不匹配EXIT
也不匹配(name: String, actor: Actor)
的消息,将使用case msg
。换句话说,case msg
是一种处理所有不匹配消息的通用处理程序。Actor.self
是一个 从演员体内引用演员实例的可靠方式(建议使用self
而不是this
,以引用正确的实例)。在您的情况下,您不是从演员调用Actor.self
,因此它会失败。act()
runs recursively, unless you send anEXIT
message. For any other message that neither matchEXIT
nor(name: String, actor: Actor)
,case msg
will be used. In other wordscase msg
is a kind of a generic handler that processes all the unmatched messages.Actor.self
is a reliable way to refer to the actor instance from within an actor's body (it's recommended to useself
instead ofthis
, to refer to the right instance). In your case, you're callingActor.self
not from an actor, and, thus, it fails.仅当新消息可用时才会调用
react
内的代码。因此,如果您在case
子句中递归调用act()
,actor 将“等待”直到发送新消息(不会阻塞线程)。当您想要根据 Actor 收到的内容来更改 Actor 的行为时,它是循环的替代方案。在这里,当您收到“EXIT”
时,您将停止等待消息。self
应该在每次想要使用this
时在 Actor 的内部使用。The code inside
react
is called only when a new message is available. Thus, if you call recursivelyact()
inside acase
clause, the actor will "wait" until a new message is sent (without blocking a thread). It's an alternative toloop
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"
.self
is supposed to be used inside an actor everytime you want to usethis
.