无法解释在 scala shell 中执行的示例的行为

发布于 2024-12-21 07:06:36 字数 1005 浏览 0 评论 0原文

LS

我刚刚使用 Odersky 等人的“Scala 编程”来学习 Scala。

在关于演员的章节中,我遇到了无法解释的行为。更具体地说,当尝试向“self”发送消息时(另请参阅示例< /a>)

输入 Scala shell:

scala> import scala.actors.Actor._  
import scala.actors.Actor._
scala > self ! "Hello"

scala > self.receive { case x => x }

但最后一行没有“返回”预期的答案:

resX:Any = hello

需要执行 Ctrl-C 以使 shell 重新接受我的输入并返回消息:

Execution interrupted by signal.

scala> self.receive {case x => x}
// She's gone rogue, captain! Have to take her out!
// Calling Thread.stop on runaway Thread[Thread-54,5,main] with offending code:
// scala> self.receive {case x => x}

但是以下内容实际上有效:

self ! "Hello" ; self.receive { case x => x }

我的问题是:

发生了什么事?为什么第一个示例不起作用而第二个示例起作用?!? 我想更好地理解 shell 的行为,因为这本书的作者声称使用 self 作为参与者答案的接收者是一种很好的调试技术。

L.S.

I'm just learning Scala using "Programming in Scala" by Odersky et al.

In the chapter on actors I ran in to behaviour I can't explain. More specifically when trying to send a message to 'self' (see also example)

Entering in the Scala shell:

scala> import scala.actors.Actor._  
import scala.actors.Actor._
scala > self ! "Hello"

scala > self.receive { case x => x }

But the last line doesn't 'return' with the expected answer:

resX:Any = hello

In need to do a Ctrl-C to get the shell back accepting my input and returns the message that:

Execution interrupted by signal.

scala> self.receive {case x => x}
// She's gone rogue, captain! Have to take her out!
// Calling Thread.stop on runaway Thread[Thread-54,5,main] with offending code:
// scala> self.receive {case x => x}

But the following actually works:

self ! "Hello" ; self.receive { case x => x }

My questions is:

What's happening? Why doesn't the first example work and the second does?!?
I would like to understand the behaviour of the shell a bit better as the authors of the book claim that using self as the recipient of answers of actors is a nice debugging technique.

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

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

发布评论

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

评论(1

嘿哥们儿 2024-12-28 07:06:36

在本书撰写时,只有一种演员:基于线程的演员。因此,如果在同一线程的不同对象上调用 self,它会返回相同的 Actor。从 Scala 2.8(我认为)开始,这不再成立。

shell 的工作方式是为每个发送的输入创建匿名类,因此在第一种情况下,

object $1 {
  self ! "Hello"
}

object $2 {
  self.receive { case x => x }
}

$1.self$2.self 不同,而在第二种情况下你有的情况

object $3 {
  self ! "Hello" ; self.receive { case x => x }
}

At the time the book was written, there was only one sort of actors: thread-based actors. So if self was called on different objects from the same thread, it returned the same Actor. Since Scala 2.8 (I think) this no longer holds.

The way shell works is by creating anonymous classes for each sent input, so in the first case you have

object $1 {
  self ! "Hello"
}

object $2 {
  self.receive { case x => x }
}

and $1.self is different from $2.self, while in the second case you have

object $3 {
  self ! "Hello" ; self.receive { case x => x }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文