我在使用仿制药和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] {
// ...
}