使用案例类和参与者键入不匹配(字符串) [英] Type mismatch (String) using case class and actor
问题描述
当我尝试引用代码时出现某些格式问题,因此,图片如下:(
Some format issue when I'm trying to quote the code, so here is the picture :(
import akka.actor.{Actor, ActorSystem, Props}
case class Number(n: Int)
case class String(s: String)
class DoublingActor extends Actor {
def receive: Receive = {
case Number(n) => println(s"Result of doubling $n: ${n*2}")
case String(s) => println(s"Result of doubling $s: ${s}${s}")
}
}
object Double extends App {
val system = ActorSystem("DoublerSystme")
val doubler = system.actorOf(Props[DoublingActor], "doubler")
doubler ! Number(5)
doubler ! String("test")
}
问题是,演员可以很好地处理数字,但是如何添加返回两次字符串的匹配函数?
The thing is, the actor works fine with number, but how can I add the matching function that returns the string twice?
推荐答案
您看到了什么行为?由于您的问题被包装在图像中,因此无法确定是否进行测试,但是我希望您收到描述性消息 object的编译错误,字符串不是case类,也不是它有一个unapply / unapplySeq成员
。
What behavior are you seeing? Due to your issue being wrapped in an image, I can't test for sure, but I'd expect that you are getting a compile error with the descriptive message object String is not a case class, nor does it have an unapply/unapplySeq member
.
因为它没有 unapply
成员,您需要将语句修改为
Because it doesn't have the unapply
member, you'll need to modify your statement to
case s: String => println(s"Result of doubling $s: ${s}:${s}")
这篇关于使用案例类和参与者键入不匹配(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!