是否可以匹配通用案例类? 我们的想法是使用一些通用案例类作为演员之间的消息。
多数民众赞成我的方法:无论如何都有可能实现这一目标吗?
case class EbTreeDataObject[T](uId: Long, changeId: Long, payload:Option[T])
case class InsertNewObject[T](newObject: EbTreeDataObject[T])
case class UpdateObject[T](changedObject: EbTreeDataObject[T])
class TreeActor[T](maxSynchroWait:Int,communication:CommunikationLayer[T]) extends Actor {
var uIdTree = new EbTree[EbTreeDataObject[T]]
var changeIdTree = new EbTree[EbTreeDataObject[T]]
override def receive: Receive = {
//basic tree operations
case InsertNewObject(newObject: EbTreeDataObject[T]) =>
//insert element in tree
case UpdateObject(newObject: EbTreeDataObject[T]) =>
//update object
case RemoveObject(removedObject: EbTreeDataObject[T]) =>
//remove object
这种结构在编译时会出错:
Error:(32, 37) pattern type is incompatible with expected type;
found : model.EbTreeDataObject[T]
required: model.EbTreeDataObject[Any]
Note: T <: Any, but class EbTreeDataObject is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
case InsertNewObject(newObject: EbTreeDataObject[T]) =>
感谢您的帮助
答案 0 :(得分:4)
为什么不做编译器建议的事情:
case class EbTreeDataObject[+T](uId: Long, changeId: Long, payload: Option[T])