加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala案例类在receive方法中不匹配(在akka actor中)

发布时间:2020-12-16 09:51:16 所属栏目:安全 来源:网络整理
导读:我有这样的事情: class ProbeActor extends Actor { case class Probe(messageKey: String) def receiveProbe: Receive = { case Probe(probeKey) = println("Good probe: "+probeKey) case x = println("Bad probe: "+ x) } final override def receive =
我有这样的事情:

class ProbeActor extends Actor {
  case class Probe(messageKey: String)
  def receiveProbe: Receive = {
    case Probe(probeKey) => println("Good probe: "+probeKey)
    case x => println("Bad probe: "+ x)
  }
  final override def receive = receiveProbe orElse receiveOther
  def receiveOther: Receive = {
    case _ => println("Other")
  }
}

我称之为:

class Prober extends ProbeActor {
  val definite = ActorSystem("ProbeTest").actorOf(Props[ProbeActor],name = "probed")
  implicit val timeout = Timeout(5 second)
  val future = definite ? Probe("key")
}

我希望打印文本“Good probe:key”,但我得到“Bad probe:Probe(key)”.

注意:如果我将Probe案例类放在外面,那么它可以正常工作.

解决方法

经过更多搜索,我在 scala-lang.org找到了答案:

I think the underlying misconception is about the identity of nested
class types.

In

class A { class B }

each new instance of class A x creates a new type x.B. And if you do the plain pattern match inside of A you are referring to the specific instance of the type B this.B.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读