与Akka演员正确差异



我在使用仿制药和akka演员时经常遇到以下问题:

trait AuctionParticipantActor[P <: AuctionParticipant[P]]
  extends StackableActor {

  override def receive: Receive = {
    case message: Handled =>
      participant = participant.handle(message)
      super.receive(message)
    case message =>
      super.receive(message)
  }
  protected var participant: P
}

AuctionParticipantActor只是周围AuctionParticipant周围的包装器。我需要P类型是协变量的,我不确定什么是实现这一目标的最佳方法。

另外,对于我的用例,我什至不需要参数化AuctionParticipantActor。我可以有类似的东西:

trait AuctionParticipantActor
  extends StackableActor {

  override def receive: Receive = {
    case message: Handled =>
      participant = participant.handle(message)
      super.receive(message)
    case message =>
      super.receive(message)
  }
  protected var participant: AuctionParticipant[???]
}

但是在这种情况下,我不确定该代替什么???为了尊重绑定的类型。如果有人认为我的问题与设计有关,请这样说。想法?

如果您不使用F-bond-bound-polymormorlism,为什么您需要AuctionParticipant才能通用?然后,AuctionParticipant[P]中类型参数P的含义是什么?如您所说,AuctionParticipantActor只是AuctionParticipant上的包装器,并且如果AuctionParticipantActor不再是通用的,那么也许AuctionParticipant也不应该是。

trait AuctionParticipantActor
  extends StackableActor {

  override def receive: Receive = {
    case message: Handled =>
      participant = participant.handle(message)
      super.receive(message)
    case message =>
      super.receive(message)
  }
  protected var participant: AuctionParticipant
}
trait AuctionParticipant {
  // ...
}

否则,如果AuctionParticipant仍然应该是通用的(即P还有其他含义),那么也许您可以使用存在类型:

trait AuctionParticipantActor
  extends StackableActor {

  override def receive: Receive = {
    case message: Handled =>
      participant = participant.handle(message)
      super.receive(message)
    case message =>
      super.receive(message)
  }
  protected var participant: AuctionParticipant[_]
}
trait AuctionParticipant[P] {
  // ...
}

相关内容

  • 没有找到相关文章

最新更新