Scala actor 如何返回一个值来响应消息?
有很多参与者向发件人回复另一条消息的示例,但在浏览 API 文档时,我注意到!!和 !?属于 CanReply 特征一部分的运算符(这似乎是 2.8 中的新增内容: http://www.scala-lang.org/archives/rc-api/scala/actors/CanReply.html)。因此,我想知道这是否只是让接收/反应块返回一个值的情况,即使 PartialFunction 返回类型不是 Unit 的类型?
我将开始深入研究源代码,尝试弄清楚它们的用途,但如果有人有任何见解或知道任何更深入的文档或示例,那么我将不胜感激。
干杯, 保罗.
There are plenty of examples of actors replying with another message back to the sender, but whilst browsing the API docs I noticed the !! and !? operators which are part of the CanReply trait (which seems to be new to 2.8: http://www.scala-lang.org/archives/rc-api/scala/actors/CanReply.html). I was therefore wondering whether it was just a case of having the receive/react block return a value, i.e. make the PartialFunction return type something other than Unit?
I'll start digging through the source to try to work out how they're meant to be used, but if anyone has any insight or knows of any more in-depth documentation or examples then I'd be most grateful.
Cheers,
Paul.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用
reply
方法发送回复,如下所示:可以通过三种方式显式接受回复。
!!
,它返回一个Future
,它是一个容器类,承诺在您需要时为您提供内容。它立即返回,但如果您实际请求内容,则必须等到另一个线程完成并填充请求。!?
没有超时。只要其他线程需要回复,您的代码就会暂停。!?
。您的代码将暂停,直到收到回复或超时到期(以先到者为准)。这是所有三个的示例:
如果你运行这个,你会得到
Replies can be sent with the method
reply
, as shown here:There are three ways to explicitly accept the reply.
!!
, which returns aFuture
, which is a container class that promises to give you the contents when you need them. It returns immediately, but if you actually ask for the contents, you have to wait until the other thread is done and fills the request.!?
without a timeout. Your code will pause for as long as it takes for the other thread to reply.!?
with a timeout. Your code will pause until it gets a reply or until the timeout expires, whichever comes first.Here's an example of all three:
And if you run this you get